Forum: Ruby on Rails partial variable help

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.
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-03-29 15:35
Can't work this out:

In list.rhtml:
<% for employee in @employees -%>

<%= render(:partial => "list_form", :object => employee) %>

<% end -%>

In _list_form.rhtml (partial):
<%= employee.id %>

In controller:
@employees = Employee.find(:all)

I get the following error:
undefined local variable or method `employee'

I everything is correct as far as I can see, just no variables are
getting through?
Fbef770cd388e77fb4103777d6dd722b?d=identicon&s=25 Joseph (Guest)
on 2006-03-29 16:24
James Whittaker wrote:
> Can't work this out:
>
> In list.rhtml:
> <% for employee in @employees -%>
>
> <%= render(:partial => "list_form", :object => employee) %>
>
> <% end -%>
>
> In _list_form.rhtml (partial):
> <%= employee.id %>
>
> In controller:
> @employees = Employee.find(:all)
>
> I get the following error:
> undefined local variable or method `employee'
>
> I everything is correct as far as I can see, just no variables are
> getting through?

From Rails API Docs:

Rendering partials
Partial rendering is most commonly used together with Ajax calls that
only update one or a few elements on a page without reloading. Rendering
of partials from the controller makes it possible to use the same
partial template in both the full-page rendering (by calling it from
within the template) and when sub-page updates happen (from the
controller action responding to Ajax calls). By default, the current
layout is not used.

# Renders a collection of the same partial by making each element of
@wins available through
# the local variable "win" as it builds the complete response
render :partial => "win", :collection => @wins

E.g From my app:

list.rhtml
<% if !@contacts.empty? %>
  <%= render :partial => 'contacts', :collection => @contacts, :locals
=> { :hidden => false } %>
<% end %>

_contacts.html
<%= contact.name %>

Hope this helps

/Joseph
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-03-29 16:34
I believe that when you send a variable to a partial with the :object
argument, it takes on the name of the partial.

In your example, that would mean the variable would be called
"list_form".

If all else fails, you can use the :locals argument, like so:

render(:partial => "list_form", :locals => { :employee => employee })

Jeff

James Whittaker wrote:
> Can't work this out:
>
> In list.rhtml:
> <% for employee in @employees -%>
>
> <%= render(:partial => "list_form", :object => employee) %>
>
> <% end -%>
>
> In _list_form.rhtml (partial):
> <%= employee.id %>
>
> In controller:
> @employees = Employee.find(:all)
>
> I get the following error:
> undefined local variable or method `employee'
>
> I everything is correct as far as I can see, just no variables are
> getting through?
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-03-29 17:06
Jeff Coleman wrote:
>
> I believe that when you send a variable to a partial with the :object
> argument, it takes on the name of the partial.
>
> In your example, that would mean the variable would be called
> "list_form".
>
> If all else fails, you can use the :locals argument, like so:
>
> render(:partial => "list_form", :locals => { :employee => employee })
>
> Jeff
>
> James Whittaker wrote:
>> Can't work this out:
>>
>> In list.rhtml:
>> <% for employee in @employees -%>
>>
>> <%= render(:partial => "list_form", :object => employee) %>
>>
>> <% end -%>
>>
>> In _list_form.rhtml (partial):
>> <%= employee.id %>
>>
>> In controller:
>> @employees = Employee.find(:all)
>>
>> I get the following error:
>> undefined local variable or method `employee'
>>
>> I everything is correct as far as I can see, just no variables are
>> getting through?


Yes I have just realised that it takes the name of the partial as the
local variable name.

I have ran into another issue. I am using link_to_remote to update a
form via ajax. However I need it to replace the div with a partial.
However when pressed it jumps me to a blank screen? no error or
anything.
This topic is locked and can not be replied to.