# Questions on this controller

**URL:** <https://www.ruby-forum.com/t/questions-on-this-controller/193275>\
**Category:** Rails\
**Created:** [August 14, 2010, 4:32pm UTC](https://www.ruby-forum.com/t/questions-on-this-controller/193275 "2010-08-14T16:32:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 14, 2010, 4:32pm UTC](https://www.ruby-forum.com/t/questions-on-this-controller/193275/1 "2010-08-14T16:32:29Z")

</div>

At: [Getting Started with Rails — Ruby on Rails Guides](http://edgeguides.rubyonrails.org/getting_started.html)

Under: 7.4 Generating a Controller

I have some questions about this controller:

class CommentsController \< ApplicationController  
def create  
@post = Post.find(params[:post\_id])  
@comment = @post.comments.create(params[:comment])  
redirect\_to post\_path(@post)  
end  
end

- @comment = @post.comments.create(params[:comment])

Does this statement mean: Create a new post comment passing it the  
CONTENTS of the new comment?

- redirect\_to post\_path(@post)

The browser will be redirected to post\_path. But, what does the object  
@post represent? Is it the CURRENT post we are at?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 14, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/questions-on-this-controller/193275/2 "2010-08-14T16:48:32Z")

</div>

On 14 August 2010 15:32, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> redirect\_to post\_path(@post)  
> end  
> end
> 
> - @comment = @post.comments.create(params[:comment])
> 
> Does this statement mean: Create a new post comment passing it the  
> CONTENTS of the new comment?

That’s correct. Note that the attributes for the new comment (the  
content, if you like) are coming from the parameters on this request  
(params[:comment]) – in other words, the information that was  
submitted via a form.

> - redirect\_to post\_path(@post)
> 
> The browser will be redirected to post\_path. But, what does the object  
> @post represent? Is it the CURRENT post we are at?

post\_path is a URL helper that gets defined when you declare  
‘resources :posts’ in your routes.rb. It expects one parameter (in  
this case, you’ve given it @post), and it runs the #to\_param method on  
this to get an ID value to insert into the generated URL. By default,  
models return their ID when you call #to\_param on them, so if @post  
has an ID of 7, for example, then post\_path(@post) will generate:

/posts/7

As for what @post is, it’s not just some magic variable. If you look  
earlier in your code, you actually define @post in the line:

@post = Post.find(params[:post\_id])

This ‘create’ action is being called via a URL like this:

/posts/:post\_id/comments

so you’re looking up a Post object based on the :post\_id that was  
passed in in the URL. So, yes, in this case @post will contain the  
‘current’ post, but only because you’ve written the code yourself to  
make this happen. What you’ve got there is a common pattern for nested  
resources, but do remember that @post isn’t being automatically  
defined for you by Rails – you have to look up the Post object  
yourself.

Chris
