2017-09-21 13 views
-1

を使用しているとき、私は方法があります。未定義のメソッドのエラー送信()メソッド

def complete_sign_in(user, redirect_path, flash_type, message) 
     sign_in user 
     remember user 
     flash[send(flash_type.to_sym)] = message 
     redirect_to send(redirect_path.to_s) 
    end 

私はこのように呼び出す:うまく動作'register_optional_path'何らかの理由で

complete_sign_in(@user, 'register_optional_path', 'success', 
        t(:account_activated)) 

を、私は設定しようとすると、 'success'にフラッシュのタイプ、それは私にエラーを与える:

NoMethodError: undefined method `success' for ... 

それはそのように振る舞う理由を任意のアイデアと方法それを修正するには?

答えて

0

sendを使用しないと、このようなエラーを回避できます。 redirect_pathflash_typeの文字列の値を渡すときは、to_symを使用してリダイレクト先のルートを「作成」し、flash[:success]キーを文字列または記号キーとしてアクセスできるため、使用することなく直接アクセスできます.to_sも、送信することさえありません。

であなたが試みることができる:それでは、あなたが必要な値にハッシュを渡すことによって、それを呼び出すことができます

def sign_and_remember(user) 
    [method(:sign_in), method(:remember)].each { |method| method.call(user) } 
end 

def complete_sign_in(options={}) 
    sign_and_remember(options[:user]) 
    flash[options[:flash]] = options[:message] 
    redirect_to options[:path].to_sym 
end 

:私はあなたのような、少しあなたの方法を微調整することができると思い

def complete_sign_in(user, redirect_path, flash_type, message) 
    sign_in user 
    remember user 
    flash[flash_type] = message 
    redirect_to redirect_path.to_sym 
end 

options = { 
    flash: 'success', 
    message: t(:account_activated), 
    path: 'register_optional', 
    user: @user 
} 
complete_sign_in(options) 
0

successというメソッドはありません。しかし、とにかくこれに文字列や記号を渡さなければなりません。

このようにそれを書き直し

def complete_sign_in(user, redirect_path, flash_type, message) 
    sign_in user 
    remember user 
    flash[flash_type.to_sym] = message 
    redirect_to send(redirect_path.to_s) 
end 

私もredirect_path変数を書き換え、直接メソッドを送信するためにあなたをお勧めします。このよう

:今すぐことができます

def complete_sign_in(user, redirect_path, flash_type, message) 
    sign_in user 
    remember user 
    flash[flash_type.to_sym] = message 
    redirect_to redirect_path 
end 

このように呼び出す:

complete_sign_in(@user, register_optional_path, 'success', t(:account_activated)) 
関連する問題