2

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.

2
  • I'm slightly confused. So home.html.erb renders product_table.html.erb which renders product.html.erb which renders _product.html.erb? Commented Dec 23, 2013 at 17:08
  • home.html.erb renders _product_table.html.erb and the _product_table.html.erb renders _product.html.erb Commented Dec 23, 2013 at 17:13

1 Answer 1

2

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 %>
Sign up to request clarification or add additional context in comments.

2 Comments

yes I tried that then I in href tag I did something like this. <div class="product" data-ref="<% ref %>"><a href="<%somevalues%>" data-ref = "<%ref%>">></div> but the values of data attribute did not append in the url on mouse hover which we see in the bottom left corner of chrome
Use <%= ref %> instead of <% ref %>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.