【问题标题】:Rails :ActionView::Template::Error (undefined method `model_name' for NilClass:Class): [duplicate]Rails :ActionView::Template::Error(NilClass:Class 的未定义方法“model_name”):[重复]
【发布时间】:2011-04-22 13:26:29
【问题描述】:

可能重复:
MetaSearch “undefined method `model_name' for NilClass:Class” for global bar search

这是我在尝试访问用户个人资料或单个帖子 (/posts/1) 时使用更新后的代码时遇到的错误。奇怪的是它仍然试图访问不同的部分,因为它是一个没有任何喜欢的新用户。

我得到的代码与教程中显示的完全一样,我曾经对以下用户执行此操作。它适用于关注用户的用户,但我已经设置它试图与喜欢帖子的用户一起做。

ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
    1: <%= form_for(current_user.appreciations.find_by_liked_id(@user),
    2:                                    :html => { :method => :delete },
    3:                                 :remote => true) do |f| %>
    4:   <div class="actions"><%= f.submit "Unlike" %></div>
  app/views/users/_unlike.html.erb:1:in     `_app_views_users__unlike_html_erb___769256097635878617_2160255900__1293121959744503098'
  app/views/users/_like_form.html.erb:6:in `_app_views_users__like_form_html_erb___3308190581058867471_2162903180_52867338942162503'
  app/views/posts/show.html.erb:7:in `_app_views_posts_show_html_erb___3188789470217885182_2183453320__3624460432667345580'

这是几乎所有东西的馅饼,如果我错过了什么,请告诉我。我真的很感激让这项工作发挥作用的洞察力。

http://pastie.org/1824955

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 entity-relationship relationship


    【解决方案1】:

    没有给@post 变量赋值。您尚未发布包含上述任何部分的视图的代码,但我假设您正在迭代 @posts 中的帖子,可能会将每个帖子分配给名为 post 的局部变量。您大概应该将 post 变量作为局部 ..., :locals =&gt; {:post =&gt; post} 传递给局部变量,然后在局部变量中引用 post,而不是 @post

    【讨论】:

    • 我已经在上面添加了用户的显示视图,很抱歉我发布这个问题时已经很晚了。我仍然对将每个变量分配给名为 post 的局部变量的位置感到困惑?我在哪里放置 :locals => 代码以将 post 变量作为本地传递给局部变量?
    • 我尝试添加 {:post => post} if user_signed_in? %> 到我的 _post.html.erb 但变量没有被传回:(
    • 不应该是 'users/like_form', ... 吗?
    • 是的,我确实尝试过,但在它不起作用后,我回滚了我的提交并重新开始,这次我得到了一个不同的错误。我已经包含了我的所有代码,感谢任何帮助。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    相关资源
    最近更新 更多