Render partial and passing in the object

I am having a little problem with the rendering of partials and form
helpers. Either i am missing something or this just cannot be done. Here
is
my contrived example…

here is the partial _address.rhtml

<%= text_area( ‘address’, ‘street’) %>

Now if i want to render this i do like so…

#get a user object
@user = User.find_by_id(1)
@address = User.address
render :partial=>‘address’}

this produces…

Front St.

Now if i want to pass in the object to the partial…

#get a user object
@user = User.find_by_id(1)
render :partial=>‘address’, :object=>User.address}

this produces…

In the second example where i passed in the object, i can access it and
print values by doing like so in _address.rhtml

<%= address.street %>

I want to be able to stick to using the form helpers, but still pass in
the
object without having to declare @address. Is this possible?

Any ideas besides putting this line in the partial?:
<% @address = address %>

Thanks in advance for any help,

mark


Mark Van H.
[email protected]
http://lotswholetime.com


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Mark Van H. wrote:

Now if i want to pass in the object to the partial…

#get a user object
@user = User.find_by_id(1)
render :partial=>‘address’, :object=>User.address}

there is no :object
User.address isnt on an object
you want @user.address
and the } hanging out by itself is an error.

the form you want is:

Renders the same partial but also makes a local variable available

to it
render :partial => “win”, :locals => { :name => “david” }

or in your case

render :partial => ‘address’, :locals => { :address => @user.address }
see

http://api.rubyonrails.com/classes/ActionController/Base.html#M000171

Hi !

2005/11/9, Jeff S. [email protected]:

render :partial => ‘address’ :locals => {:address => User.address}

That won’t work either, because the form helper functions expect an
instance variable to be available. It has nothing to do whatsoever
with having or not a partial in the picture.

Hope that helps !
François

<% @address = address %>

is the basic of it.

That won’t work either, because the form helper functions expect an
instance variable to be available. It has nothing to do whatsoever
with having or not a partial in the picture.

That is correct. I guess my subject line was a little off base. It is
actully the form helper that is expecting the instance variable. Is
there a
way to change what the form helper looks at?

Mark


Mark Van H.
[email protected]
http://lotswholetime.com