Nested partial, locals, and NameError


#1

I have a _form partial shared by new and edit actions. I render the
partials like so:

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

The partial looks (more or less) like this:

<%= f.error_messages %>
...

<table id="recipe_ingredients">
  <tr>
    <th>ingredient</th>
    <th>in units</th>
    <th>quantity</th>
    <th>delete?</th>
  </tr>
<% f.fields_for :recipe_ingredients do |ri_f| %>
  <%= render :partial => 'recipe_ingredient', :locals => { :ri_f

=> ri_f } %>
<% end %>

As you can see, I’m rendering the _recipe_ingredient partial from the
_form partial, assigning the ri_f value to the :ri_f key. The partial
looks like this:

<tr id="<%= "recipe_ingredient-#{ri_f.object.id }"%>">
  <td>
    <%= ri_f.collection_select :ingredient_id,

Ingredient.all, :id, :name %>


<%= ri_f.select :unit, RecipeIngredient::COOKING_UNITS %>


Problem: when I call one of the actions, I get the following error:

ActionView::TemplateError (undefined local variable or
method `ri_f' for #<ActionView::Base:0xb681ecbc>) on
line #1 of app/views/recipes/_recipe_ingredient.html.erb:
1: <tr id="<%= "recipe_ingredient-#{ri_f.object.id }"%>">
2:   <td>
3:     <%= ri_f.collection_select :ingredient_id,

Ingredient.all, :id, :name %>
4:

    app/views/recipes/_recipe_ingredient.html.erb:1
    app/views/recipes/_form.html.erb:21
    app/views/recipes/_form.html.erb:20
    app/views/recipes/new.html.erb:4
    app/views/recipes/new.html.erb:3
    app/controllers/recipes_controller.rb:30:in `new'

I have googled around but no luck so far… The syntax and all seems
ok, but it is refusing to work. If I move the code from the second
partial into the _form partial, it all works.

Any idea what I should correct?


Branko


#2

Solved. Had a link_to function using the same partial (in the code I
snipped) with no locals assigned. It’s fixed now.


Branko