undefined method for nil:NilClass, identical contr

2019-08-04 15:44发布

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:

undefined method 'count' for nil:NilClass

View:

<%= @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

Any ideas?

Thanks!!

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

Also, here is the error generated in the terminal:

Started GET "/conversations/trashbin" for 127.0.0.1 at 2014-01-02 17:38:34 -0500 Processing by ConversationsController#trashbin as HTML User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 Rendered conversations/trashbin.html.erb within layouts/application (4.0ms)

1条回答
祖国的老花朵
2楼-- · 2019-08-04 16:30

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 -_-.

Sorry for wasting your time everyone!

查看更多
登录 后发表回答