1

エラー: (nilのための強調テキスト未定義のメソッド `プロファイル」:NilClass)ページ番号ホームにNoMethodErrorルビー/ Railsの - ページの#ホームでNoMethodError - パーシャルビュー

enter image description here


コントローラー:pages_controller.rb

class PagesController < ApplicationController 
before_action :authenticate_user!, except: [:landing, :home, :plan] 

def home 
    @contributor_plan = Plan.find(1) 
    @elitecontributor_plan = Plan.find(2) 
    @technician_plan = Plan.find(3) 
    @elitetechnician_plan = Plan.find(4) 
    @center_plan = Plan.find(5) 
    @elitecenter_plan = Plan.find(6) 
    @affair_plan = Plan.find(7) 
    @eliteaffair_plan = Plan.find(8) 
end 

答えて

1

問題は@usernilであり、nilオブジェクトではprofileメソッドを呼び出すことはできません。よく見ると

、あなたのbefore_filter

before_action :authenticate_user!, except: [:landing, :home, :plan] 

は(exceptで指定)homeアクションのauthenticate_user!メソッドを呼び出していません。したがって、現在ログインしているユーザーにはアクセスできません。

したがって、homeアクションのためにユーザーを認証する必要があります。 、また、あなたのbefore_filter

before_action :authenticate_user!, except: [:landing, :plan] 

に変更をあなたは現在ログインしているユーザーを見つけることができたと current_userと呼ばれるメソッドを公開考案。したがって、あなたのビューは current_userであり、 @userではありません。

<%= image_tag current_user.profile.avatar.url %> 
+0

私はbefore_filterを改訂しました。ありがとうございました。ただし、エラーメッセージが残ります。私はpages_controllerで "user"を適切に定義していないと仮定します。これは正しいでしょうか? –

+0

@NathanielRand私の答えを更新しました。見て、それが問題を解決するかどうかを教えてください。 –

+0

問題は解決されました。あなたの貢献は評価されます。したがって、Deviseの「current_user」はどの「authenticate_user!」にも適用できます。ページ? –

関連する問題