我正在尝试使用 before 过滤器来防止未经批准的用户访问页面。
为了检查未经批准的用户,我在 /lib 文件夹中的 .rb 文件中定义了一个方法(我有几个方法,我在各种控制器和视图中)。方法是 check_user_for_current_approval(user_id)。
在我的控制器中,我尝试了以下变体(请注意,我使用 RubyMine 作为 IDE,它并没有提示我有语法错误):
before_filter(:only => [:new]) do |c|
c.check_user_for_current_approval current_user.id
end
这给了我以下错误消息:
NoMethodError in PaymentsController#new
private method `check_user_for_current_approval' called for #<PaymentsController:0x007ff06784f148>
以下语法:
before_filter(:only => [:new]) do |c|
c.send (:check_user_for_current_approval, current_user.id)
end
产生此错误:
SyntaxError in PaymentsController#new
/app/controllers/payments_controller.rb:9: syntax error, unexpected ',', expecting ')'
c.send (:check_user_for_current_approval, current_user.id)
^
/app/controllers/payments_controller.rb:9: syntax error, unexpected ')', expecting keyword_end
/app/controllers/payments_controller.rb:130: syntax error, unexpected $end, expecting keyword_end
使用此语法:
before_filter(:only => [:new]) { check_user_for_current_approval(current_user.id) }
将我直接带到页面(过滤器无效)。不同的陈述来自我在 StackOverflow 上看到的答案。