Parameter passing

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 ?

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. <
[email protected]> wrote:


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

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.

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
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. <
[email protected]> wrote:

href="javascript:document.dyna_form.submit()";><img


Posted via http://www.ruby-forum.com/.


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

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)” [email protected]

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()”;> -->



<%= image_submit_tag("/images/create.jpg")%>
 
<% 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.

Thanx for the information. It was a great help

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs