Rails Rails 4.1 可能导致某些 Filter 失效

cassiuschen · 2014年03月07日 · 最后由 wudixiaotie 回复于 2014年07月11日 · 3285 次阅读

做 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?

还没 release 的。。。。。

#1 楼 @pynix Ummm....beta 不就是让大家看看有什么 bug、相关部件跟进的意思么…………

#2 楼 @cassiuschen 差不多这个意思吧,,,不是 major version 升级应该改动不会太多吧。。

#3 楼 @pynix 是么= =!我一直以为 major version 相比之前的 beta 就是改改最后汇报的一些 bug 然后 clean up 然后发布…………

大版本升级就是 rails 3 升到 rails 4。。。。。

#5 楼 @pynix 哦………………但这些细节改动也是坑啊………… 😢

ransack 要用 4 的分支;i18n 有问题要调(比如用 github 库代码,以及 rails-i18n 库)

#8 楼 @as181920 i18n 的问题正式发布应该可以解决……谢谢!

我他们真实天才,这个问题我自己发现的,没想到你也发现了。。。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号