私はEvents
コントローラを持っていますが、イベントが公開されている場合は認証をスキップします。条件付きでskip_before_filterを適用します:if =>条件をレールで指定します。
私ApplicationController
では、私はのauthenticate_user!
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
を考案するには、このコールは今、私のイベントテーブルの中に、私はpublic
と呼ばれるブール値フィールドを持っています。私はイベントが公開されているかどうかを確認するためにそれを使用します。このようにEventsController
class EventsController < ApplicationController
skip_before_action :authenticate_user!, only: :show, if: Proc.new { :is_public? }
end
しかし、何らかの理由でこれが機能しませんでした。私はこれをしなければならなかった。期待し、上記スキップした後の逆の状態とbefore_filter
を繰り返しているため@event.public = true
場合は認証をスキップとして
class EventsController < ApplicationController
skip_before_action :authenticate_user!, only: :show
before_action :authenticate_user!, unless: :is_public?
def is_public?
@event.present? && @event.is_public
end
end
これは動作します。
私は疑問に思って:私がやったこと
- が正しいのですか?
- パフォーマンスへの影響はありますか。はいの場合は、良い方法がありますか?
...起こっているので、多くのbefore_action事がある理由を理解することはかなり難しいということでしょうか? 'Proc.new {:is_public}' – Damien
Nopes。私は実際にこの ':if => conditonal'を動作させることができませんでしたので、それが':is_pulic? 'か':is_public'であるかどうかは分かりません – CuriousMind
'public'フィールドがある場合、 「公衆」または「公的? 'is_public?'メソッドを 'Event'モデルに移動します。 – Damien