# Nested partial, locals, and NameError

**URL:** <https://www.ruby-forum.com/t/nested-partial-locals-and-nameerror/168437>\
**Category:** Rails\
**Created:** [May 20, 2009, 7:57pm UTC](https://www.ruby-forum.com/t/nested-partial-locals-and-nameerror/168437 "2009-05-20T19:57:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Branko\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/779978/32.png) [@Branko\_V](https://www.ruby-forum.com/u/Branko_V)\
**Post date:** [May 20, 2009, 7:57pm UTC](https://www.ruby-forum.com/t/nested-partial-locals-and-nameerror/168437/1 "2009-05-20T19:57:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Branko\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/779978/32.png) [@Branko\_V](https://www.ruby-forum.com/u/Branko_V)\
**Post date:** [May 20, 2009, 10:17pm UTC](https://www.ruby-forum.com/t/nested-partial-locals-and-nameerror/168437/2 "2009-05-20T22:17:05Z")

</div>

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

–  
Branko
