Forum: Ruby on Rails render partial and passing in the object

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.
3bef3fc4d0cbcf58f3d66c106e2fd16f?d=identicon&s=25 mvette13 (Guest)
on 2005-11-12 12:49
(Received via mailing list)
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...

<textarea id="address_street" name="address[street]">Front
St.</textarea>

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...

<textarea id="address_street" name="address[street]"></textarea>

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

<textarea><%= address.street %></textarea>

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 Holstyn
mvette13@gmail.com
http://lotswholetime.com
7d3039b18b2d4db92a13937f09446c9c?d=identicon&s=25 rails_lists (Guest)
on 2005-11-12 12:49
(Received via mailing list)
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
C6c6d0d2160a6811ff4409d678c11db9?d=identicon&s=25 sean (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Mark Van Holstyn 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/ActionControlle...
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 francois.beausoleil (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Hi !

2005/11/9, Jeff Smick <rails_lists@hardcorpsfans.org>:
> 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
3bef3fc4d0cbcf58f3d66c106e2fd16f?d=identicon&s=25 mvette13 (Guest)
on 2005-11-12 12:49
(Received via mailing list)
>
>
> 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 Holstyn
mvette13@gmail.com
http://lotswholetime.com
C6c6d0d2160a6811ff4409d678c11db9?d=identicon&s=25 sean (Guest)
on 2005-11-12 12:49
(Received via mailing list)
<% @address = address %>

is the basic of it.
This topic is locked and can not be replied to.