【问题标题】:Rails: NoMethodError in ItemsController#showRails:ItemsController#show 中的 NoMethodError
【发布时间】:2017-10-10 17:57:16
【问题描述】:

从我的ItemsController 访问@favorites = current_viewer.favorites_items.where(item_id: @item.id) 时收到错误消息:

NoMethodError in ItemsController#show 
undefined method `favorites_items' for nil:NilClass

items_controller.rb

 def show
   @favorites = current_viewer.favorites_items.where(item_id: @item.id)
   @comments = Comment.where(item_id: @item).order("created_at DESC")
   @items = Item.find(params[:id])
 end

模型关联:

class Viewer < ApplicationRecord
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  has_many :comments, dependent: :destroy
  has_many :favorites
 has_many :favorite_items, through: :favorites, source: :favorited, source_type: 'Item'

end

class Favorite < ApplicationRecord
  belongs_to :viewer
  belongs_to :favorited, polymorphic: true
end

class Item < ApplicationRecord
  belongs_to :seller
  belongs_to :category
  has_many :comments, dependent: :destroy

  mount_uploaders :attachments, ImageUploader
end

routes.rb

devise_for :viewers, controllers: {registrations: 'viewers/registrations', sessions: 'viewers/sessions'}
  devise_scope :viewer do
    get "viewers/index"=> "viewers/sessions#index", :as => "viewer_index"
  end

get '/favorites', to: 'favorite_items#index', as: 'favorites'
resources :favorite_items, only: [:create, :destroy]

更新 1

接下来我用 byebug 输入了三遍:

40: 
41:   # GET /items/1
42:   # GET /items/1.json
43:   def show
44:     byebug
=> 45:     @favorites = current_viewer.favorites_items.where(item_id: @item.id)
46:     @comments = Comment.where(item_id: @item).order("created_at DESC")
(byebug)

in /.rvm/gems/ruby-2.4.0/gems/actionpack-5.1.4/lib/action_controller/metal/rescue.rb
17: 
18:     private
19:       def process_action(*args)
20:         super
21:       rescue Exception => exception
=> 22:         request.env["action_dispatch.show_detailed_exceptions"] ||= show_detailed_exceptions?
23:         rescue_with_handler(exception) || raise
24:       end
25:   end
26: end
(byebug) 

18:     private
19:       def process_action(*args)
20:         super
21:       rescue Exception => exception
22:         request.env["action_dispatch.show_detailed_exceptions"] ||= show_detailed_exceptions?
=> 23:         rescue_with_handler(exception) || raise
24:       end
25:   end
26: end
(byebug) 

【问题讨论】:

  • 你没有定义current_viewer
  • 既然您使用的是设计,您应该使用 current_viewer
  • @Gabriel Mesquita...这只是问题的错字,我现在改了
  • 是的,我看到了。 current_viewer 是 nil 你需要定义它
  • @Gabriel Mesquita... 我该如何定义current_viewer

标签: ruby-on-rails ruby-on-rails-4 devise


【解决方案1】:

由于您有不同类型的用户角色,您需要像这样正确访问您的助手:

def show
   if current_viewer
    @favorites = current_viewer.favorites_items.where(item_id: @item.id)
   elsif
    @favorites = current_seller.favorites_items.where(item_id: @item.id)
   end
   @comments = Comment.where(item_id: @item).order("created_at DESC")
   @items = Item.find(params[:id])
 end

current_viewer 助手为 nil,因为您是作为卖家登录的!

【讨论】:

  • 我不得不删除if current_viewer 上的? 出现提示此错误的错误...现在elsif 行出现错误:undefined method 'favorites_items' for #&lt;Seller:0x007fa22fbb6dd0&gt;
  • @Theopap 查看器有这个 has_many :favorite_items, through: :favorites, source: :favorited, source_type: 'Item' , 卖家也有吗?
  • 我还必须将 favorites_items 更改为 favorite_items,我得到了这个:undefined method `favorite_items' for nil:NilClass... 嗯,很奇怪
  • 有道理,因为你有这个: has_many :favorite_items 。不是收藏夹 :) 这个 nilClass 发生在观众或卖家身上?
【解决方案2】:

您可能必须在过滤器之前设置以验证查看器

class ItemsController < ActionController::Base
  before_action :authenticate_viewer!

  def show
   ...
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多