Bazley
Bazley

Reputation: 2847

Passing variable to partial inside partial

In the view.html.erb:

<%= render @posts %>

In each _post.html.erb:

<%= render partial: 'shared/block', locals: { blockedcallsign: post.user.callsign } %>

In each _block.html.erb:

<%= button_to blockrelationships_path(
                params: { 
                  blocker_callsign: @user.callsign, 
                  blocked_callsign: blockedcallsign #'baz'
                },
                remote: true
              ),
              class: 'btn btn-default btn-xs' do %>
  <span class="glyphicon glyphicon-ban-circle" aria-hidden="true"></span>
<% end %>

In _block.html.erb I get the error: undefined local variable or method 'blockedcallsign'. When I replace blockedcallsign with a string like 'baz' it works. For some reason blockedcallsign isn't being passed from _post.html.erb to _block.html.erb. What's wrong with the code?

Upvotes: 0

Views: 69

Answers (3)

Bazley
Bazley

Reputation: 2847

There's nothing wrong with the code. There was a forgotten second 'render' in view.html.erb causing the problem. Sorry.

Upvotes: 0

ABMagil
ABMagil

Reputation: 5595

The syntax you're looking for is

render 'shared/block', locals: {blockedcallsign: post.callsign} in _posts.html.erb

Then, in _block.html.erb, you need to call blockedcallsign not @blockedcallsign

Upvotes: 1

Rubyrider
Rubyrider

Reputation: 3587

The latest way is:

<%= render 'shared/block', { blockedcallsign: post.callsign } %>

Pass as hash.

Upvotes: 1

Related Questions