local variables in partials lead to NameError

I know I must be doing something dumb, but I can't seem to send a local variable to my partial. The below code leads to the following error:

NameError in Residential_listings#new undefined local variable or method `foo' for #<ActionView::Base: 0x3fb53f8>

I am trying to call the variable 'foo' in a partial called _attachment.html.erb, which is nested in another partial called _form.html.erb. In _form.html.erb, I have: <%= render( :partial => 'listings/attachment',       :collection => @residential_listing.attachments,       :locals => {:foo => "bar"}) %>

And then in /listings/_attachment.html.erb, I simply have: <%= foo>

This causes the error above. Can anyone help me figure out what I'm doing wrong?

Jamie

does

<%= foo %>

work?

Sorry that was a typo in my original message. I have <%= foo %> in my partial and it doesn't work.

Hi Jamie,

Without seeing more code, I'd guess you're passing a local to that partial without passing the value you're assigning to the local to the view / partial above. But that's just a guess. Post some additional code for additional assistance.

Hi Bill,

I could certainly post more code, but first can I ask what you mean by 'without passing the value you're assigning to the local'? In the render call, the locals hash clearly has a value ("bar") for the key :foo. Or am I missing something obvious?

Thanks, Jamie

I should also say that I have partial locals in other parts of my code that are working fine, as in my edit.html.erb:

<%= render :partial => 'form', :locals => {:residential_listing => @residential_listing} %>

Hi Jamie,

> Without seeing more code, I'd guess you're passing a local to that > partial without passing the value you're assigning to the local to the > view / partial above. But that's just a guess. Post some additional > code for additional assistance.

Hi Bill,

I could certainly post more code, but first can I ask what you mean by 'without passing the value you're assigning to the local'? In the render call, the locals hash clearly has a value ("bar") for the key :foo. Or am I missing something obvious?

My bad. Passing a literal should work fine.

I'm a little confused by the error / code you posted which is why I suggested you post additional info.

The error message says the error is occurring in Residential_listings#new. The partials you're rendering are in 'listings', not 'residential_listings' as one would expect. Also, I'd not expect 'Residential_listings'. That is, the Rails standards would lead me to expect 'ResidentialListings' as an object name. Anyway, post some more code and the full error listing you're getting and maybe we can help.

Best regards, Bill

Thanks for your help Bill,

The partial is in a folder called views/listings because it is shared by two different models, ResidentialListing and CommercialListing, both of which inherit (via STI) from a Listing model. The error is occuring in Residential_listings#new because that is the template I am requesting, namely: http://localhost:3000/residential_listings/new. That template calls a _form partial, which in turn calls the listings/ _attachment partial. Anyway, so here's some more code:

First the _form partial (in the folder views/residential_listings):

<%= error_messages_for 'residential_listing' %> <% form_for @residential_listing, :html => { :multipart => true } do | f> %>

  <p>     <%= f.label :title %><br />     <%= f.text_field :title %>   </p>   <p>     <%= f.label :description %><br />     <%= f.text_area :description, :class => "mceEditor" %>   </p>   <p>      <%= f.label :building_id %><br />     <select name="residential_listing[building_id]">       <% @buildings.each do |building| %>         <option value="<%= building.id %>">           <%= building.name %>         </option>       <% end %>     </select>     <%= submit_tag "Manage Buildings", :name => "buildings" %>   </p>   <p>      Apt. #<br />      <%= f.text_field :address %>   </p>   <p>     <%= f.check_box :show_real_address %> Show Address (if unchecked, listing will showspace_type cross-streets)   </p>   <p>     <%= f.label :price %><br />     <%= f.text_field :price %>   </p>   <p>     Apartment Type<br />     <%= select( :residential_listing, :space_type, { "Studio" => "Studio",                               "1 Bedroom" => "1 Bedroom",                               "2 Bedroom" => "2 Bedroom",                               "3 Bedroom+" => "3 Bedroom+"}) %>   </p>   <p>     BR: <%= f.text_field :beds, :size => 5 %> / BA: <%= f.text_field :baths, :size => 5 %>   </p>   <p>     <%= f.check_box :broker_fee %> Broker Fee?   </p>   <p>     <%= f.label :agent_id %><br />     <select name="residential_listing[agent_id]">     <% @agents.each do |agent| %>       <option value="<%= agent.id %>">         <%= agent.full_name %>       </option>     <% end %>     </select>     <%= submit_tag "Manage Agents", :name => "agents" %>   </p>   <p>     <%= f.label :status %><br />     <%= select( :residential_listing, :status, { "Inactive" => "inactive", "Active" => "active"}) %>   </p>

  <div id="attachments">     Images     <%= render( :partial => 'listings/attachment',           :collection => residential_listing.attachments,           :locals => { :foo => "bar"}) %>   </div>

  <p>     <%= add_image_link "Add Image" %>   </p>   <p>     <%= f.submit "Save Changes" %>   </p> <% end -%>

Aha! Posting the full error text led me to read it more carefully. There's a line in there that says:

app/helpers/listings_helper.rb:3:in `add_image_link'

Looked in add_image_link, which is a helper that dynamicall adds more copies of the same partial. Needed to add the local variables there too, and now all is golden! Thanks again for your help.

def add_image_link(name)     link_to_function name do |page|       page.insert_html :bottom, :attachments, :partial => 'listings/ attachment', :object => Attachment.new, :locals => {:foo => "bar"}     end   end

Hi Jamie,