做 cas 登陆的时候用到了rubycas-client
,结果在 4.1 下怎么都说缺少before
方法,退回到 4.0.x 之后却没有一点问题。
曾一度以为是before_filter
统一成before_action
搞的鬼,但发觉它只是改了名字而已。
今天狠心翻 commit,才惊觉原来 4.1 中before_action
要求 Filter 拥有一个before
方法,后来查EdgeGuide才知道原来 guide 里早就这么写了……
可能很多人早就知道了……这里摘那么一段:
The second way is to use a class (actually, any object that responds to the right methods will do) to handle the filtering. This is useful in cases that are more complex and cannot be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login filter again to use a class:
class ApplicationController < ActionController::Base before_action LoginFilter end
class LoginFilter def self.before(controller) unless controller.send(:logged_in?) controller.flash[:error] = "You must be logged in to access this section" controller.redirect_to controller.new_login_url end end end
>Again, this is not an ideal example for this filter, because it's not run in the scope of the controller but gets the controller passed as an argument. The filter class must implement a method with the same name as the filter, so for the before_action filter the class must implement a before method, and so on. The around method must yield to execute the action.
大概就是说使用这种通过class定义的filter都必须得用相同名称的方法,不管是before啊还是after神马的……
`rubycas-client`改好了:
`gem 'rubycas-client', github: 'cassiuschen/rubycas-client', branch: 'feature/UpdateToRails4.1' `
……顺便大家都汇报一下4.1下有问题的gem?