# Basic n00b question

**URL:** <https://www.ruby-forum.com/t/basic-n00b-question/144622>\
**Category:** Rails\
**Created:** [July 31, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/basic-n00b-question/144622 "2008-07-31T16:56:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [July 31, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/basic-n00b-question/144622/1 "2008-07-31T16:56:02Z")

</div>

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| %\>

Name: \<%= f.text\_field :author%\>

Email: \<%= f.text\_field :email%\>

\<%= f.text\_area :body%\>

\<%= f.submit 'submit' %\>
\<% 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.

---

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [July 31, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/basic-n00b-question/144622/2 "2008-07-31T17:10:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [July 31, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/basic-n00b-question/144622/3 "2008-07-31T17:28:56Z")

</div>

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?
