userkhan
userkhan

Reputation: 66

Passing instance variables in nested partials in Rails?

I am having difficulty passing instance variables in nested partials. Here is what I have done.

In controller Own:

def home
  @item = "some values"
  @ref = "some other values"
end

Then I have a home page "home.html.erb", in which I rendered "_product_table.html.erb":

<%= render "own/product_table", :item => @items, :ref => @ref %>

Then, in "_product_table.html.erb", I have to render "_product.html.erb":

<% @items.each do |item|%>
  <%= render "own/product", :item => item, :ref => @ref %>
<% end %>

I can't understand why the ref variable is not available in the "_product.html.erb" partial.

Upvotes: 2

Views: 1064

Answers (1)

Gergo Erdosi
Gergo Erdosi

Reputation: 42073

You are passing ref as argument to the partial in home.html.erb, so in _product_table.html.erb you should use it similarly to item, not as instance variable:

<% @items.each do |item|%>
  <%= render "own/product", :item => item, :ref => ref %>
<% end %>

Upvotes: 2

Related Questions