Rails :ActionView::Template::Error (NilClass:Class 的未定义方法“model_name”):

发布于 2024-11-03 04:19:42 字数 1388 浏览 3 评论 0原文

可能的重复:
MetaSearch“NilClass 的未定义方法‘model_name’: Class” 用于全局栏搜索

这是当我尝试访问用户的个人资料或单个帖子 (/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

Possible Duplicate:
MetaSearch “undefined method `model_name' for NilClass:Class” for global bar search

This is the error I'm getting using my updated code when I try going to a user's profile or a individual posts (/posts/1). Its weird that its trying to access the unlike partial anyway because its a new user without any likes.

I've got the code exactly as it shows in the tutorial I used to do this with following users. It works fine for users following users, but I've set it up trying to do with users liking posts.

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'

Here's the pastie with just about everything, if I missed something just let me know. I really appreciate the insight to get this working.

http://pastie.org/1824955

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲墙角沉默 2024-11-10 04:19:42

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

Nothing assigns a value to the @post variable. You have not posted the code for the view(s) that include(s) any of the partials above, but I assume you are iterating over the posts in @posts, probably assigning each one to a local variable called post. You should presumably be passing that post variable to the partial as a local ..., :locals => {:post => post}, and then referring to post, not @post within the partials.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文