Ruby on rails 控制器未在\u筛选器之前继承
所以我有ApplicationController.rb:Ruby on rails 控制器未在\u筛选器之前继承,ruby-on-rails,ruby,actioncontroller,Ruby On Rails,Ruby,Actioncontroller,所以我有ApplicationController.rb: class ApplicationController < ActionController::Base protect_from_forgery def decode_email params[:email] = URI::decode(params[:email]) end end class UsersController < ApplicationController before_fil
class ApplicationController < ActionController::Base
protect_from_forgery
def decode_email
params[:email] = URI::decode(params[:email])
end
end
class UsersController < ApplicationController
before_filter :decode_email, only: [:show]
def show
#blah blah
end
end
class ApplicationController
然后是UsersController.rb:
class ApplicationController < ActionController::Base
protect_from_forgery
def decode_email
params[:email] = URI::decode(params[:email])
end
end
class UsersController < ApplicationController
before_filter :decode_email, only: [:show]
def show
#blah blah
end
end
class UsersController
现在,点击show action将导致:
undefined local variable or method 'decode_email' for #<UsersController:0x007fb5f216a710>
未定义的局部变量或方法“解码电子邮件”#
为什么不继承该方法,以便将其正确用作before\u筛选器?class ApplicationControllerclass ApplicationController < ActionController::Base
protect_from_forgery
private
def decode_email
params[:email] = URI::decode(params[:email])
end
end
保护你不被伪造
私有的
def解码电子邮件
params[:email]=URI::decode(params[:email])
结束
结束
对我有用这应该有用。难道
decode\u email
被误认为是一种私人方法吗?您的应用程序控制器中是否有private
?它不工作完全是因为private
-它应该是私有的。您使用的是哪个ruby和rails版本?你尝试重新启动你的服务器了吗?@QumaraSixOneTour,不,它不应该是私有的!它应该是公共的或受保护的,但不是私有的。看起来它应该可以工作,但是可以尝试在ApplicationController.rb
中的方法上方添加helper\u方法:decode\u email
。