未定义的局部变量 post

Undefined local variable post

我写这段代码:

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

然后在_post.htm.erb我写了下面的代码:

<div class="post-title">
  <img src="<%= @user.avatar.url%>" class="img-rounded post-image">
  <h4 id="post-name"><%= @user.first_name + ' ' +@user.last_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>

当我转到此页面时,我看到以下内容:

undefined local variable or method `post'

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} %>

你需要使用 partial: 当你将 locals 传递给 partial 如下:

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

希望对您有所帮助。