I followed the rails tutorial by michale hartl, I then wanted to extend it, so I attempted to integrate the thumbs_up gem. I was able to get voting working for both posts and comments, and was very proud. But then I wanted to work on my UI a little. I didn’t like that all the forms for adding a new comment was below the feed of comments attached to the post. So I thought that all i had to do was change the order of my partials
from this
<%= render partial: 'comments/comment', collection: my_item.comments, as: :comment %>
<%= render :partial => "comments/form", :locals => { :cur_post => my_item } %>
to this
<%= render :partial => "comments/form", :locals => { :cur_post => my_item } %>
<%= render partial: 'comments/comment', collection: my_item.comments, as: :comment %>
and it would work. But this was sadly not the case. When I reorder them I get this error.
No route matches {:action=>"vote_up", :controller=>"comments", :id=>#<Comment id: nil, content: nil, user_id: nil, post_id: 14, created_at: nil, updated_at: nil>}
I am able to remove either partial and everything will work, so I am unsure why having the form above the comments causes an error.
Here is a link to a gist
any help would be great.
The issue is coming from this line I believe
When this occurs first, it builds a new Comment onto cur_post.comments. The problem occurs now, when you render “comments/comment” that newly built comment isn’t saved. Rails expects when you pass a model into a url helper like this:
That the object is in the db (AFAIK)
You should be able to get around this by not building into the
commentsassocaitionotherwise, in your comments/comment you could check to see if
comment.new_record?is true and deal with it accordingly.