Hello, my website has the ability for users to post top level comments successfully, but I am having much trouble with comment children. I am using polymorphic associations, so the comment model can apply to Articles, Profiles, and Pictures. In each of the three cases, the comment will see the model as "commentable".
I want to create a reply link next to each individual comment which passes in the "id" of that particular comment into the comments "new" view page. Then I want the comments "create" method to use that id, @top_level_comment = Comment.find(id), and make a new child using the parameters that the user specified in the "new" comment view, @top_level_comment.children.create(params[:comment]), but I do not know how to put it into code.
So, the user will start on an article show page, which has the following controller:
def show
@article = Article.find(params[:id])
@commentable = Article.find(params[:id])
@comments = @commentable.comments
@comment = Comment.new
@title = @article.title
end
Then, he will scroll down to the comments on the bottom, and see that there is a reply link via the article show page:
<div id="comment <%= comment.id %>">
<%= comment.title %>
| <%= link_to "Permalink", polymorphic_path([@commentable, comment]), :action => :show %>
| <%= link_to "Reply", polymorphic_path([@commentable, @comment]), :action => :new %>
| <%= link_to "Edit Comment", polymorphic_path([@commentable, comment]), :action => :edit %>
| <%= link_to 'Delete Comment', [[@commentable, comment]], :confirm => "Are you sure?", :method => :delete %><br />
<%= comment.content %><br />
<%= comment.user.name %><br />
<%= @comment.children.count %><br /><br />
<%= render :partial => 'shared/comment', :collection => @comment.children %>
</div>
It is this line in particular where I am having problems:
| <%= link_to "Reply", polymorphic_path([@commentable, @comment]), :action => :new %>
How do I change this to do what I want to do from the description at the top of this post? The polymorphic path does not work. I think it does not work because commentable only works with Articles, Profiles, and Pictures, but not comments. How do I change the path, so it goes to the comments "new" page while passing on the id of the current comment(comment.id)?
Also, my form for adding new comments looks like this:
<%= form_for([@commentable, @comment]) do |f| %>
<%#= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit "Post Comment" %>
</div>
<% end %>
Here is what the routing looks like:
resources :articles do
resources :comments
end
Hello, when I follow mark's routing, I get this when I try to view an article:
No route matches {:controller=>"comments", :action=>"create", :article_id=>#<Article id: 300, title: "Filler Title", content: "Sunt sit est incidunt et.", user_id: 6, created_at: "2010-09-08 17:42:10", updated_at: "2010-09-08 17:42:10">}
Added the following new info on September 16, 3:48 PST:
Here's the form that allows the user to comment on a commentable, but it does not work when the @commentable is a comment.
1: <%= form_for([@commentable, @comment]) do |f| %>
2: <%#= render 'shared/error_messages', :object => f.object %>
3: <div class="field">
4: <%= f.label :title %><br />
comments controller "new"
def new
@commentable = find_commentable
@comment = Comment.new
end
When replying to a comment, it goes to the above, but it does not know what @commentable is, so its value is nil when replying to a comment. How do I make it so that @commentable is the @comment from which the user pressed reply from? Here is the link that allows the user to reply to a comment:
| <%= link_to "Reply", new_comment_path(comment.children.new) %>
Thank you very much.