2011-12-30 11 views
0

ruby​​でオペレータ& &について何か不足しているはずです(この動作の原因は疑わしい)。RoR 3:before_filterとコントローラのアクションで同じコードが動作しない

このコード: 匿名& & user_signed_in? は、それがapplication_controllerのbefore_filterまたはホームコントローラのアクションにある出力を生成しません。

そして私はなぜunderstantません。

詳細:

Iは、アプリケーションコントローラでこのコードを追加し、それがあろうラン

before_filter
anonymous = cookies['anonymous'] 
fresh_session = cookies['session_id'].nil? 
rep = (fresh_session || (anonymous && user_signed_in?)) 
puts "REPONSE : #{rep} session: #{fresh_session} anonym #{anonymous} is there a curr user: #{user_signed_in?} /// BOTH: #{(anonymous && user_signed_in?)}" 

として印刷resulteである:

REPONSE : true session: false anonym false is there a curr user: true /// BOTH: true 

場合同じコードがホームコントローラの動作にある th e印刷結果は

REPONSE : false session: false anonym false is there a curr user: true /// BOTH: false 

私は理解してください。

編集(Klochneの要求に続いて):app_controllerで

:home_controllerで

Anon-class: String REPONSE : true session: false anonym false is there a curr user: true /// BOTH: true 

Anon-class: FalseClass REPONSE : false session: false anonym false is there a curr user: true /// BOTH: false 

ので1が何らかの理由で、それを文字列として解釈します。

+0

「anon-class:#{anonymous.class.name}」 – klochner

+0

何かが、コードを呼び出して検索/印刷する前にクッキー['anonymous']を 'false'に設定していますあなたがApplicationControllerでそれを呼び出すと、それは文字列に設定されています。これは '&&'演算子とは関係ありません。 – Batkins

+0

私はそのようなものだったと思うが、私はそれを明示的にfalseまたはtrueに設定した唯一の場所以外は何も設定しない。 – Emanuel

答えて

0

は、インスタンス変数の代わりに、ローカル変数として前フィルターで変数を設定してみてください。そうすれば、インスタンス変数とは対照的にコントローラの動作を維持したままにしておくことができます。

@anonymous = cookies['anonymous'] 
@fresh_session = cookies['session_id'].nil? 
@rep = (fresh_session || (anonymous && user_signed_in?)) 
puts "REPONSE : #{@rep} session: #{@fresh_session} anonym #{@anonymous} is there a curr user: #{user_signed_in?} /// BOTH: #{(@anonymous && user_signed_in?)}" 

そして、あなたのコントローラであなたがもう一度、このラインを置くことができます:

puts "REPONSE : #{@rep} session: #{@fresh_session} anonym #{@anonymous} is there a curr user: #{user_signed_in?} /// BOTH: #{(@anonymous && user_signed_in?)}" 
をそして、それは同じ結果をプリントアウトする必要がありますので、このようにbefore_filterで呼び出されるメソッドを変更します。 Local variables are only accessible from within the block of its initialization

関連する問題