nil:NilClass的未定义方法“ microposts”

分数

我正在阅读Michal Hart的教程并研究Rails4。我花了几个小时来解决这个问题。

user_controller.erb:

class UsersController < ApplicationController
def show
    @user = current_user
    @microposts = @user.microposts
  end
end

micopost_controller.erb:

class MicropostsController < ApplicationController



 def create
    @micropost = Micropost.create(micropost_params)

  end

  private
  def micropost_params
    params.require(:micropost).permit(:content) if params[:micropost]

  end

end

show.html.erb:

<h2>Users Post</h2>
<% if @user.microposts.any? %>
    <table class="microposts" summary="User microposts">
        <%= render @microposts %>
    </table>

_micropost.html.erb:

<tr>
<td class="micropost">
        <span class="content">
            <%= micropost.content %>
        </span>
        <span class="timestamp">
            Posted <%= time_ago_in_words(micropost.created_at) %> ago.
        </span>
    </td>
</tr>

错误截图

甚至“ undefined method`microposts' ”也帮不了我。

任何帮助将不胜感激。

佛罗兹
@user = current_user # this is returning nil

错误“ nil:NilClass的未定义方法'foo'”的意思是:

“嘿,您在某个地方写了blahblahblah.foo,但是blahblahblahis的值nil,它没有foo方法。”

在这种情况下,你可曾致电唯一的一次.microposts@user.microposts,这意味着@usernil我们看到它被分配了结果current_user,因此该方法正在返回nil

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章