我有一些用于两个控制器的常用过滤器。过滤器完全相同,但两个控制器中的跳过条件不同。
在 u_controller 中:
before_filter :require_login,:get_notification,:get_credits
skip_before_filter :require_login , :only => [:index,:registration,:login,:oauthlogin,
:forgotpassword,:invite_mail,:newpassword,
:changepassword,:invite,:verify,:terms,:privacy,:about]
skip_before_filter :get_notification , :only => [:registration,:login,:oauthlogin,
:forgotpassword,:invite_mail,:newpassword,
:changepassword,:invite,:verify]
skip_before_filter :get_credits , :only => [:registration,:login,:oauthlogin,
:forgotpassword,:invite_mail,:newpassword,
:changepassword,:invite,:verify,:terms,:privacy,:about]
在 q_controller 中:
before_filter :require_login,:get_notification,:get_credits,:require_auth
skip_before_filter :get_notification,:get_credits,:require_auth,
:only => [:topic_suggest,:upload,:new_question,:qdb,:post_new_question,:evaluate_answer,:image]
skip_before_filter :require_auth, :only => [:add]
在这种情况下,解决以下问题的最佳做法是什么:
- 避免在两个控制器中重复声明过滤器
- 使用更简洁的代码而不是
skip_before_filter
.