Basic n00b question

I have a model called comment which has the typical stuff like belongs_to :post, author, email, body and a posts controller with the method add_comment.

On the views/posts/show.html.erb I have:

<% form_for @comment, :url => {:action => :add_comment} do |f| %>   <div>Name: <%= f.text_field :author%></div>   <div>Email: <%= f.text_field :email%></div>   <div><%= f.text_area :body%></div>   <div><%= f.submit 'submit' %></div> <% end %>

How do I wire it up so that when I'm on localhost:3000/posts/24/ and hit the submit button, it will either: A) Add a new comment with the right post_id B) Redirect back to localhost:3000/posts/24/ with an error message if all required fields are not filled out.

Look into making comments a nested resource of your posts. This will keep your comments in the context of a post.

Your route would look something like: /posts/24/comments

Using a POST on that URI would call your comments_controller create action.

Wow thanks for the quick response.

I've tried the nested routes route. (map.resources :posts, :has_many => :comments)

I need /posts/24/comments to look exactly the same as /posts/24, except with error messages for validating the comment fields. However, I get this weird artifact where the comment I filled out partially shows up on /posts/24/comments.

Also, comments only needs add and delete so I figured I should make it a model without a controller and just have posts_controller do the add and admin_controller do the delete. Is there a way to achieve this?