I’m a new Rails programmer and I’ve been working on a simple blog
application to get started. But I have this problem with comment
validation.
I’m using REST throughout my entire app, and I have the comments
resource nested under the posts resource. I’m using a form on the show
action of my posts controller for submitting the comments. The form
goes to post_comments_path(@post.id) and the comments will submit
properly, that part works. But if the model doesn’t validate, I can’t
get the browser to reload the show action and display what the user
originally typed in on the form, along with the errors.
My main question is, how do I display the errors if the form is under
a different controller (as well as “save” what the user types in)?
Here’s my form that displays on the show action:
<% form_for [:post, @comment], :url => post_comments_path(@post.id) do
|f| %>
Add comment:
<%= error_messages_for :comment %>
<%= f.text_field :author %> Name
<%= f.text_field :email %> Email (will not be displayed)
<%= f.text_area :body, :rows => 10 %>
<%= submit_tag 'Add comment' %>
<% end %>And here’s my create action in the comments controller:
def create
@comment = Comment.new(params[:comment])
if @post.comments << @comment
redirect_to post_path(@post.id)
else
# I don’t know what to put here
end
end
Any help would be much appreciated.