dopplesoldner dopplesoldner - 4 months ago 16
Ruby Question

Rails 4 - passing variable to partial

I am following the Ruby on Rails tutorial and have come across a problem while trying to pass variables to partials.

My

_user
partial is as follows

<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
</li>


I would like to pass in a number for the size value. I am trying as follows without any luck.

<%= render @users, :locals => {:size => 30} %>


Thanks in advance!

Answer

You need the full render partial syntax if you are passing locals

<%= render @users, :locals => {:size => 30} %>

Becomes

<%= render :partial => 'users', :collection => @users, :locals => {:size => 30} %>

Or to use the new hash syntax

<%= render partial: 'users', collection: @users, locals: {size: 30} %>

Which I think is much more readable