Hello,
I have added a comment form to my article show page, but after I submit
the comment, I get the following error: Couldn't find Article without an
ID.
I think it is because the Comments controller does not know which
article to add the new comment.
comments_controller:
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(params[:comment])
if @comment.save
flash[:success] = "Comment saved."
redirect_to articles_path(@article)
else
render 'show'
end
end
article show page:
<% @article.comments.each do |comment| %>
<%= comment.title %>
<%= comment.content %>
<%= comment.user %>
<% end %>
<%= form_for([@article, @article.comments.build]) 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 %>
I am trying to follow this guide:
http://edgeguides.rubyonrails.org/getting_started.html#adding-a-second-model
Thanks.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment