Rails:ActionView :: Template :: Error(NilClass:Class的未定义方法“ model_name”): [英] Rails :ActionView::Template::Error (undefined method `model_name' for NilClass:Class):

查看:262
本文介绍了Rails:ActionView :: Template :: Error(NilClass:Class的未定义方法“ model_name”):的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


这是我使用更新后的错误尝试访问用户个人资料或单个帖子(/ posts / 1)时输入代码。奇怪的是,它还是试图访问不同的部分,因为它是一个没有任何喜欢的新用户。与以下用户进行此操作。它对于关注以下用户的用户很好用,但我已将其设置为与喜欢帖子的用户一起使用。

  ActionView :: Template :: Error(对于NilClass:Class,未定义的方法 model_name):
1:< ;%= form_for(current_user.appreciations.find_by_liked_id(@user),
2::html => {:method =>:delete},
3::remote => true)做| 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_view ___ s_users_330_162_b_b_b_b_b_b_b / 6 / in_app_view_form__s_users_230_b_b views / posts / show.html.erb:7:`_app_views_posts_show_html_erb ___ 3188789470217885182_2183453320__3624460432667345580'

关于一切,如果我错过了什么,请告诉我。我非常感谢能使这项工作奏效的见识。



http://pastie.org/1824955

解决方案

没有内容为 @post 变量分配值。您尚未发布包含以上任何部分的视图的代码,但我认为您正在遍历 @posts 中的帖子将每个变量分配给一个名为 post 的局部变量。您应该应该将 post 变量作为局部 ...,:locals =>传递给局部变量。 {:post => post} ,然后在分部中指的是 post ,而不是 @post


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

解决方案

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.

这篇关于Rails:ActionView :: Template :: Error(NilClass:Class的未定义方法“ model_name”):的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆