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.