Rails :ActionView::Template::Error (NilClass:Class 的未定义方法“model_name”):
可能的重复:
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'
这是几乎所有东西的馅饼,如果我错过了什么,请告诉我。我真的很欣赏让这项工作发挥作用的洞察力。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有任何内容为
@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 calledpost
. You should presumably be passing thatpost
variable to the partial as a local..., :locals => {:post => post}
, and then referring topost
, not@post
within the partials.