Forum: Ruby on Rails partials and objects...?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Josh (Guest)
on 2007-01-19 16:22
(Received via mailing list)
I have this one partial _ownerselect.rhtml in my Computers Controller
that has an instance var @computers.

I call the partial like:

   :partial =>' ownerselect', :object => computers

>From inside the partial computers.location does not work.  The only
working solution is ownerselect.location

Why on earth has the name of the partial replace that of the object
passed to it?

Thanx!
Chris H. (Guest)
on 2007-01-19 16:45
(Received via mailing list)
when using the :object option, the name of the object in the partial
takes the name of the partial.  its part of the DRY principal in that
you can reuse the partial without requiring the partial to know the
name of the object you are passing to it.  think of it like a method
call.  the method itself is not aware of the name of the parameter on
the outside of the function.


def mymethod(arg)
   # isn't aware of the name of the argument outside the function
end

x = 1
mymethod(x)


say i have a partial named '_people_list.rhtml'

in one action i want to list all the people

@people = Person.find(:all)
render :partial => "people_list", :object => @people

in another action i want to list just blond_haired_people

@bh_people = Person.find(:all, :conditions => "hair_color = 'blonde'")
render :partial => "people_list", :object => @bh_people

then in my partial i just use people_list to refer to the object that
was passed in and it will always work.
Alan F. (Guest)
on 2007-01-19 16:49
Josh wrote:
> I have this one partial _ownerselect.rhtml in my Computers Controller
> that has an instance var @computers.
>
> I call the partial like:
>
>    :partial =>' ownerselect', :object => computers
>
>>From inside the partial computers.location does not work.  The only
> working solution is ownerselect.location
>
> Why on earth has the name of the partial replace that of the object
> passed to it?


It's not a replace, it's a local vriable in the partial, named after the
partial, which has been assigned the :object passed to it.

This allows the partial to be 'self-contained', and can be used from
many controllers etc with different variables.

Just like passing a parameter to a method...

A.
This topic is locked and can not be replied to.