Forum: Ruby on Rails Passing values to a Partial

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.
65faa79b41e0879ce4fe9f15cc61d107?d=identicon&s=25 Kent Barber (kentbarber)
on 2006-03-31 01:07
Hi,

I cannot seem to find out how to pass in a value to a partial. Can
anyone help me?

What I am doing(in the code below) is calling a function in my
controller called setobject. This function just redraws the partial. I
want to be able to change the value @objectname, redraw my partial and
have it be able to use that value. The first the page is drawn it works,
but when i call setobject it says that @objectname is null in my partial
and it throws an error.


Code follows.

In my index.rhtml I have the following:
  <%= render :partial => "properties" %>

with some other code later on in the file
<%= drop_receiving_element "dcwindow",
		      :update => "items", :url => { :action => "setobject" },
		      :accept => "products", :hoverclass => "cart-active",
		      :loading => "Element.show('indicator')",
		      :complete =>
"Element.hide('indicator');additem(element.alt,element.id);"
		      %>

In _properties.rhtml I have:
<%
    command = @objectname
%>
Which I then use later on.

In my controller file I have:
  def index
    @objectname = "Plane"
  end
  def setobject
    render :partial => "properties"
  end


Thank in advance!
Kent
858392acd1398aa6071fb1d44cc33842?d=identicon&s=25 John Smilanick (Guest)
on 2006-03-31 01:45
(Received via mailing list)
Here are some details about render():
http://api.rubyonrails.org/classes/ActionControlle...
Specifically, the Rendering Partials section.
# Renders the same partial but also makes a local variable available
to it
   render :partial => "win", :locals => { :name => "david" }

if the following change in the controller doesn't work
> render :partial => "properties", :locals => {:@objectname =>
> @objectname}
you might have to do
> render :partial => "properties", :locals => {:objectname =>
> @objectname}
and in _properties.rhtml change
> <%
>     command = @objectname
> %>
to
> <%
>     command = @objectname || objectname
> %>

-John

--
John Smilanick
Computing Staff - Webmaster
Kavli Institute for Theoretical Physics
University of California, Santa Barbara
jsmilani@kitp.ucsb.edu
(805) 893-6307
This topic is locked and can not be replied to.