Neon_10
Neon_10

Reputation: 731

Can not pass variables to partial (Rails5)

I got such a problem - there is a partial and I can not pass a variable there:

in partial I have;

<%= object.title %>

How I pass variables:

<%= render :partial => 'shared/post_preview', :locals => { :object => article } %>

the error I see looks like

**undefined local variable or method `object'**

Any ideas? I tried already everything seems...

also tried:

  <%= render :partial => 'shared/post_preview', :object => article %>
  <%= render 'shared/post_preview', :object => article %>
  <%= render :partial => 'shared/post_preview', :object => article %>

everytime i see the same error...

Upvotes: 4

Views: 2620

Answers (2)

Pradeep Sapkota
Pradeep Sapkota

Reputation: 2072

Use this:

Assuming you have defined @article instance variable in action.

<%= render 'shared/post_preview', object: @article  %>

This must solve your problem.

Upvotes: 3

Neon_10
Neon_10

Reputation: 731

the problem was in the commented code in the partial file. Somehow it was counted like an actual code...

<!--
<div class="row">
  <div class="col-lg-6">
    <%= render :partial => 'shared/post_preview' %>
    <%= render :partial => 'shared/post_preview' %>
  </div>
  <div class="col-lg-6">
    <%= render :partial => 'shared/post_preview' %>
    <%= render :partial => 'shared/post_preview' %>
  </div>
</div>
-->

Upvotes: 1

Related Questions