nil:NilClass 的未定义方法,相同的控制器操作工作正常吗? [英] undefined method for nil:NilClass, identical controller action works fine?

查看:34
本文介绍了nil:NilClass 的未定义方法,相同的控制器操作工作正常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ConversationsController 有两个相同的动作:

I have a ConversationsController with two identical actions:

 def index
    @conversations ||= current_user.mailbox.inbox.all
    @trash ||= current_user.mailbox.trash.all
  end  
  def trashbin
    @conversations ||= current_user.mailbox.inbox.all
    @trash ||= current_user.mailbox.trash.all
  end

唯一的区别是视图中显示的信息.索引页显示数据就好了.但是,对于垃圾箱视图中出现的每个实例变量,我都会收到以下错误:

The only difference is the information that is displayed in the views. The index page displays the data just fine. However, I get the following error for every instance variable that appears in the trashbin view:

未定义方法 'count' for nil:NilClass

查看:

<%= @conversations.count %>

Count 恰好是视图页面中使用的第一种方法.除了一些纯文本之外,这两个操作之间的视图几乎相同.我很困惑为什么我在一个动作上出错.我在索引页上使用 <%= @conversations.count %> 并且它工作正常.

Count just so happens to be the first method used in the view page. The views between the two actions are almost identical except for some plain text. I'm stumped as to why I'm getting an error on one action. I'm using <%= @conversations.count %> on the index page and it works fine.

唯一的可能是路由文件出错.我在使用自定义垃圾箱路由时遇到了一些麻烦,所以我认为问题出在路由文件上:

The only possibility is an error with the routes file. I had some trouble getting the custom trashbin route to work so I'm thinking the issue is with the routes file:

resources :conversations do
  member do
    post :reply
    post :trash
    post :untrash
  end
  collection do
    get :trashbin, :action => 'trashbin'
  end
end

有什么想法吗?

谢谢!!

更新路线代码(所有这些都已发布).

Updated routes code (all of it has been posted).

另外,这里是终端产生的错误:

Also, here is the error generated in the terminal:

在 2014-01-02 17:38:34 -0500 开始 GET "/conversations/trashbin" for 127.0.0.1由 ConversationsController#trashbin 处理为 HTML用户负载 (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1在布局/应用程序中呈现对话/trashbin.html.erb(4.0 毫秒)

推荐答案

结果证明这是我的菜鸟错误.控制器中的相同"操作并不完全相同.无法正常工作的控制器操作列在 Private 下,这解释了视图 -_- 无法访问实例变量的原因.

This turned out to be a rookie mistake on my part. The "identical" action in the controller wasn't exactly identical. The controller action that wasn't working properly was listed under Private, which explains why the instance variable wasn't accessible to the view -_-.

抱歉浪费大家的时间!

这篇关于nil:NilClass 的未定义方法,相同的控制器操作工作正常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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