0

I write this code:

<% @user.posts.each do |post| %>
    <%= render 'post', locals: { post: post, user: @user} %>
<% end %>

Then in _post.htm.erb I write follow code:

<div class="post-title">
  <img src="<%= @user.avatar.url%>" class="img-rounded post-image">
  <h4 id="post-name"><%= @user.first_name + ' ' [email protected]_name %> </h4>
  <div id="post-date"><%= post.created_at.strftime('%d %b - %k:%M') %></div>
</div>
<div class="post-content">
  <p><%= post.content %></p>
</div>
<%= render 'like', post:post %>
<li  class="post-divider"></li>

When I go to this page, I see follow:

undefined local variable or method `post'

2 Answers 2

1

You need to use partial: when you pass locals to a partial as follows:

<%= render partial: 'post', locals: { post: post, user: @user} %>

I hope this will help you.

Sign up to request clarification or add additional context in comments.

1 Comment

You can mark this answer as accepted if its working for you so that other users can easily find it as correct.
1

From the PartialRenderer api.

use render partial: with locals: param or "If you're not going to be using any of the options like collections or layouts, you can also use the short-hand defaults of render to render partials."

<%= render 'post', post: post, locals: { user: @user} %>

1 Comment

From the PartialRenderer api api.rubyonrails.org/classes/ActionView/PartialRenderer.html use render partial: with locals: param or "If you're not going to be using any of the options like collections or layouts, you can also use the short-hand defaults of render to render partials."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.