# Parameter passing

**URL:** <https://www.ruby-forum.com/t/parameter-passing/137980>\
**Category:** Rails\
**Created:** [May 19, 2008, 8:39am UTC](https://www.ruby-forum.com/t/parameter-passing/137980 "2008-05-19T08:39:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [May 19, 2008, 8:39am UTC](https://www.ruby-forum.com/t/parameter-passing/137980/1 "2008-05-19T08:39:18Z")

</div>

I have this 2 models in the rails application.

class Project  
has\_many :comments

class Comments  
belongs\_to :Project

i.e in the Comment table I have column name project\_id.

The model is such that for each projects there are many comments. Now I  
want the user to have the facility for adding new comments for any  
existing project.

Now my question is that how do I pass the existing project\_id for adding  
a new comment in the comments table ?

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [May 19, 2008, 9:02am UTC](https://www.ruby-forum.com/t/parameter-passing/137980/2 "2008-05-19T09:02:24Z")

</div>

Firstly,

1. Comments should be Comment. Model class names should be singular.
2. The model should belongs\_to :project, not :Project. I’m not sure if  
this  
makes a difference, but I think it reads better this way.

Now in your controller if you already have an existing project object  
initialized:

@project.comments.build(params[:comment])

This works exactly like Comment.new(params[:comment].merge({ :project\_id  
=\>  
@project.id}), just without all the ugliness.

On Mon, May 19, 2008 at 4:09 PM, Saurav C. \<  
removed\_email\_address@domain.invalid\> wrote:

> 

–  
Appreciated my help?  
Reccommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [May 19, 2008, 9:02am UTC](https://www.ruby-forum.com/t/parameter-passing/137980/3 "2008-05-19T09:02:59Z")

</div>

Oh I should re-enforce the fact that build does not create a comment  
object,  
it merely initializes a new one. You will still have to save it. That  
is,  
unless you change build to create, in which case it’ll just create it.

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [May 19, 2008, 9:39am UTC](https://www.ruby-forum.com/t/parameter-passing/137980/4 "2008-05-19T09:39:28Z")

</div>

Alright.

For this I think you should be using RESTful routing. This will stop you  
from putting editcomment and newcomment in your projects controller,  
where  
they do not belong.

Have a read of  
[frozenplague.net](http://frozenplague.net/2008/01/06/restful-routing-an-overview/) and then  
I  
think you could better understand how to do this.

On Mon, May 19, 2008 at 5:04 PM, Saurav C. \<  
removed\_email\_address@domain.invalid\> wrote:

> href="javascript:document.dyna\_form.submit()";\>\<img
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> > 
> 
> –  
> Appreciated my help?  
> Reccommend me on Working With Rails  
> [http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [May 19, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/parameter-passing/137980/5 "2008-05-19T13:12:02Z")

</div>

Whether or not you opt for RESTful routing as Ryan suggests, you need  
to understand that instances of the controllers live only as long as  
they are necessary for responding to a request. In practical terms  
that means that you cannot set an instance variable in one action and  
expect to see it available in another. That appears to be what you’ve  
done in newcomment (set @project) and editcomment (build using  
@project). Since the ProjectsController instance goes away after the  
newcomment form renders, @project also no longer exists, so  
editcomment must make it’s own.

Also, don’t miss the point that Ryan made about build – it’s similar  
to ‘new’ with some initializing done under the covers. It does not  
save anything to the db. You’ll need create to do that.

def newcomment  
@project=Project.find(params[:id])  
end

def editcomment  
@project=Project.find(params[:id])  
@project.comments.create(params[:comment])  
end

Just one comment about conventions, too. Typically ‘edit’ is used to  
retrieve an instance that already exists and return a form that can be  
used to make changes; create is used to save a new instance and update  
is used to persist changes. You are not required to follow that but  
you will find your code more consistent and easier to follow if you  
do.

On May 19, 3:39 am, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [May 19, 2008, 9:34am UTC](https://www.ruby-forum.com/t/parameter-passing/137980/6 "2008-05-19T09:34:37Z")

</div>

My main problem is Im not able to intialize the project object. Im  
explaining in detail

In my view/project/myprojects.rhtml

\<%= link\_to ‘Edit’  
,{:action=\>‘newcomment’,:controller=\>‘project’,:id=\>project}%\>

Using this file for the user to enter a new comment  
view/project/newcomment.rhtml view

\<%= render :partial =\> ‘users/user\_tabs’ %\>

\<%= error\_messages\_for :project %\>  
\<% form\_for(:project, :url =\> {:controller =\> “project”, :action =\>  
“editcomment” },  
:html =\> { :multipart =\> true, :name =\> “ref\_form”  
}) do |f| -%\>

```
        <TR>
          <TD HEIGHT=25>Reference Comment</TD>
          <TD HEIGHT=25 align="left"><%= text_area "comment", :cols 

```

=\> 20, :rows =\> 5 %\>  
  
href=“javascript:document.dyna\_form.submit()”;\> ![](https://www.ruby-forum.com/images/create.jpg) --\>

  
  

| | |
| \<%= image\_submit\_tag("/images/create.jpg")%\>  
 | &nbsp; | |

\<% end %\>

controllers/project\_controller.rb

def newcomment  
@project=Project.find(params[:id])  
end

def editcomment  
@project.comments.build(params[:comment])  
end

Now Im failing to pass the initialized @project object inside  
editcomment method

-Saurav

Im passing the project id parameter using  
Ryan B. wrote:

> Oh I should re-enforce the fact that build does not create a comment  
> object,  
> it merely initializes a new one. You will still have to save it. That  
> is,  
> unless you change build to create, in which case it’ll just create it.

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [May 21, 2008, 12:46pm UTC](https://www.ruby-forum.com/t/parameter-passing/137980/7 "2008-05-21T12:46:04Z")

</div>

Thanx for the information. It was a great help
