Forum: Ruby on Rails Nested partial, locals, and NameError

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.
Branko V. (Guest)
on 2009-05-20 21:57
(Received via mailing list)
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 %>
    </table>
    ....

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 %>
      </td>
      <td>
        <%= ri_f.select :unit, RecipeIngredient::COOKING_UNITS %>
      </td>
      ....
    </tr>

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:   </td>

        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
Branko V. (Guest)
on 2009-05-21 00:17
(Received via mailing list)
Solved. Had a link_to function using the same partial (in the code I
snipped) with no locals assigned. It's fixed now.

--
Branko
This topic is locked and can not be replied to.