Topic: How to create comments

Hello,

I'm new to this forum and Rails in general.  I'm trying to build a site that has items which can be commented on, much like a blog.  I have it to the point where I can create comments for each item, however I cannot figure out a way to set up an admin page to edit and delete them.  I can only do it through terminal with SQL commands, not very practical.  Right now I have 2 tables, one for the items and another for the comments. 

This is the code for the page where people can add comments and it will display them, however for some reason this code only works on the page I have it, I can't even get it to just show the comments on the admin page much less edit each one.

<% for comment in @site.comments %>
<%= comment.body %>
<hr />
<% end %>

<%= form_tag :action => "comment", :id => @site %>
<%= text_area "comment", "body" %><br />
<%= submit_tag "Submit" %>
</form>   

Can anybody help me with this or know of a place to learn how to do this sort of thing?

Thanks,

Dan

Re: How to create comments

You can generate the a scaffold for the Comment model. To do that, run this in the root of your rails app:

ruby script/generate scaffold Comment

This will generate a CommentsController with basic list, show, create, update, and destroy operations. Looking at the generated code might help you get an idea on how to edit and delete comments.

Also, if you haven't already, run through the first few chapters of the Rails book. The 2nd edition is still in beta, but you can download the PDF. It really is the best way to learn Rails IMO.

Railscasts - Free Ruby on Rails Screencasts

Re: How to create comments

Hello,

Thanks! I didn't think of that for some reason even though I did that for another part of the site. 

another question, I created a link that should go from the page that gives the item information to the comments page for that  item.  So I tried linking it to the list.rhtml under comments, however this gave me all of the comments for all the items.  So I tried linking it to show.rhtml with an id, but I keep getting an error saying,

Couldn't find Comment with ID=1

So somehow I need to tell it which item to show the comments for.

<span class="list-actions"><%= link_to 'Edit Site', :action => 'edit', :id => @site %>
<%= link_to 'Edit Comments', :controller => 'comments', :action => 'show', :id => @site %>
<%= link_to 'Back', :action => 'list' %><span>

Would I need to add a colum to the site table for the comment_id or something?

Thanks,

Dan

Re: How to create comments

I'm assuming you have two models, Site and Comment. Site has_many comments. Is this correct? If you want to display only the comments for a given site, you can filter the comments displayed on the list.rhtml page with, say, a select box. Here's how you might do that.

# in list.rhtml
<%= start_form_tag %>
<%= select_tag :site_id, options_from_collection_for_select(Site.find_all, :id, :name, params[:site_id])
<%= submit_tag 'Filter' %>
<%= end_form_tag %>

# in comments_controller.rb
def list
  # Filter the comments based on the selected site.
  conditions = ['site_id = ?', params[:site_id]] unless params[:site_id].nil?
  @comments = Comment.find(:all, :conditions => conditions)
end


There are plenty of other ways to do it. Another way could be to add a SitesController (possibly generated through scaffolding) where you can display the comments in the sites/show.rhtml to see all comments on a given site.

Last edited by ryanb (2006-08-01 02:45:07)

Railscasts - Free Ruby on Rails Screencasts

Re: How to create comments

Hello,

that method you gave is cool but what I'm looking for is the second way you mentioned by using the controller so that the show page only shows comments for that site. 

The projects has 2 sections, the main site and the admin.  I have it working on the main part where you can choose a site and see only the comments for that site, you can also add a comment.  So then in the admin section I have pages for each site with a link to edit comments.  I want a new page where all comments for that site are listed with an edit and delete button for each.  The same as on the list page with the first method you gave me, however if the application has a lot of sites it will be harder to use a select box to find the right one.

Does this make any sense?  I'm trying to get the show.rhtml page to only show comments for one site and be able to edit and delete each.  Right now I keep getting an error saing it can't find the Comment with out an ID.  I tried to add a comment_id column to the site table but that didn't seem to help.

Dan

PS I realize that I keep using the word site, this website deals with scuba dive sites, that's why each item is refered to a site smile

Re: How to create comments

leonard31 wrote:

Does this make any sense?  I'm trying to get the show.rhtml page to only show comments for one site and be able to edit and delete each.  Right now I keep getting an error saing it can't find the Comment with out an ID.  I tried to add a comment_id column to the site table but that didn't seem to help.

Do you have a SitesController in the admin section? If you don't, you can create one using scaffolding like you did for the comments. Then, on your sites/show.rhtml page, you can do this to show each comment with edit/destory links.

<h2>Comments</h2>
<% for comment in @site.comments %>
<div>
  <%=h comment.content %><br />
  <%= link_to 'Edit', :controller => 'comments', :action => 'edit', :id => comment %>
  <%= link_to 'Destroy', { :controller => 'comments', :action => 'destroy', :id => chapter }, :confirm => 'Are you sure?', :post => true %>
</div>
<% end %>

Does that work for you?

Railscasts - Free Ruby on Rails Screencasts

Re: How to create comments

eh, no that didn't work I still am getting the same error that it can't find the site without an ID.  I changed the structure around so the way you gave me with the select box will work well.  If I ever get this site on to a server maybe it will be easier when I can just show the site to you or anybody else, that way it will more clear what exactly what I'm trying to do.  I don't think I explain things to well yet haha.  Thanks for your help on this topic !!

Dan

Re: How to create comments

If you post the URL you are going to and the exact error message you are receiving then I might be able to figure out what's wrong. If the controllers aren't too large, pasting the code here will help too.

Railscasts - Free Ruby on Rails Screencasts