rctneil
rctneil

Reputation: 7210

locals in rails partial

I have this partial:

<% if flash.any? %>
  <% flash.each do |type, message| %>
    <%= render 'shared/flash_message', locals: { type: type, message: message } %>
  <% end %>
<% end %>

which is including this partial:

<div class="message-box message-box--<%= type %>">
  <a class="close" data-dismiss="alert">×</a>
  <%= message %>
</div>

When running that I get:

undefined local variable or method `type' for

<#:0x007fe510aa3c80>

It's pointing to an error where I am outputting <%= type %> Anyone know why?

Upvotes: 3

Views: 522

Answers (3)

Rajesh Omanakuttan
Rajesh Omanakuttan

Reputation: 6918

Actually you can render partial without using :partial if you are not passing any other options. i.e., The point to be noted is that explicitly specifying :partial is required when passing additional options such as :layout, :locals etc. So, it should be,

<% if flash.any? %>
  <% flash.each do |type, message| %>
    <%= render partial: "shared/flash_message", locals: { type: type, message: message } %>
  <% end %>
<% end %>

Ref: Using Partials

Upvotes: 0

Dave
Dave

Reputation: 4436

Try this:

<% if flash.any? %>
 <% flash.each do |type, message| %>
 <%= render :partial=>'shared/flash_message', locals: { type: type, message: message } %>
 <% end %>
<% end %>

Upvotes: 0

Sterling Graham
Sterling Graham

Reputation: 564

You have to specify that you are rendering a partial to pass in the local variables.

So change

<%= render 'shared/flash_message', locals: { type: type, message: message } %>

to

<%= render partial: 'shared/flash_message', locals: { type: type, message: message } %>.

http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables

Upvotes: 3

Related Questions