Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial not accessing local variable

I am rendering a partial like so:

 <% @pages.each do |page| %>
     <%= render 'layouts/pagewithchildren', :locals => { :page => page } %>
 <% end %>

But when i try to access a variable in page i am getting the error:

undefined local variable or method `page'

I am accessing the variable like:

<%= page.title %>

So what else do I need to do?

like image 841
Dean Avatar asked Aug 31 '12 13:08

Dean


2 Answers

i'm not 100% sure but isn't it either

<%= render 'layouts/pagewithchildren', :page => page %>

or

<%= render :partial => 'layouts/pagewithchildren', :locals => { :page => page } %>

?

like image 71
sdepold Avatar answered Jan 04 '23 04:01

sdepold


You have to explicitly specify partial, otherwise, Rails will treat locals as a params hash, you can access locals[:page] but not page variable directly in your partial.

Change your code to:

<%= render partial:'layouts/pagewithchildren', locals: {page: page} %>
like image 44
Gongqin Shen Avatar answered Jan 04 '23 05:01

Gongqin Shen