2017-10-10 7 views
1

보여레일 : ItemsController 번호에 NoMethodError 내 <code>ItemsController</code>에서 <code>@favorites = current_viewer.favorites_items.where(item_id: @item.id)</code>에 액세스 할 때 나는 오류를 수신하고

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 

경로를 .rb

01 23,516,
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] 

내가 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) 
+0

당신은'current_viewer'를 정의하지 않았습니다. – coreyward

+0

당신이 devise를 사용하고 있기 때문에 current_viewer를 사용해야합니다 –

+0

@Gabriel Mesquita ... 질문에 오타가 있습니다. 지금 바 꾸었습니다. – Theopap

답변

3

를 인증하기 위해 필터하기 전에 설정해야 할 수도 있습니다 역할을 수행하려면 다음과 같이 도우미에 올바르게 액세스해야합니다.

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입니다!

+1

if current_viewer'에서'?'를 없애고 이것이 제안되었습니다 ... 그리고 이제'elsif' 줄에 오류가 있습니다 : # <판매자 : 0x007fa22fbb6dd0>에 대한'undefined method 'favorites_items' ' – Theopap

+0

@Theopap 뷰어 has_many : favorite_items, : : 즐겨 찾기, 소스 : : 즐겨 찾기, source_type : 'Item', 판매자도 보유하고 있습니까? –

+0

'favorite_items'을'favorite_items'로 바꿔야 만했습니다 : 정의되지 않은 메소드'favorite_items 'for nil : NilClass ... hmmmm weird – Theopap

0

당신은 사용자의 다양한 종류를 가지고 있기 때문에 시청자에게

class ItemsController < ActionController::Base 
    before_action :authenticate_viewer! 

    def show 
    ... 
    end 
end