I have a blog model with has many model comments relationship.
So my form looks something like this located in the blog show view:
<% form_for [@blog, @comment] do |f| -%>
<%= f.error_messages %>
<%= f.label :message, "Add your message" %>
<%= f.text_area :message %>
<%= f.submit "Submit" %>
<% end -%>
All very straightforward.
I have one action in the comment controller and that is a create action.
def create
@comment = Comment.new(params[:comment])
respond_to do |format|
if @comment.save
flash[:success] = "Thank you for your comment"
format.html { redirect_to :back }
else
format.html { render :action => "new" }
end
end
end
My problem is that when a validation error occurs in the comment model then I will end up rendering the new comment view which doesn’t exist.
I want to render the blog show view along with the error messages.
If I try to redirect back or render the blog show view template then the users comments will end up deleted as state is not preserved between them.
Can anyone tell me what the conventional solution is to this problem? Thanks.
The solution is to have your blog accept_nested_attributes_for :comments.
By only modifying comments through the blog controller you will ensure that the you return to the blog view with validations if something goes wrong.
Changes you need to make to make it work.
Blog/show view
If there isn’t a update method in your Blog controller you will need to add on to make it work. But I believe this isn’t necessary with restful routes as rails will take the common action. However, that might also require you to have a blog edit view that is nearly identical to the show view. Instead use this code in your controller.
If you’re using attr_accessible for the Blog model you will have to add :comments to the list.