ActionController::InvalidAuthenticityToken解决途径
方法(1):
在相应的action,controller中添加下面代码
如在hr_controller.rb文件中
class HrController < ApplicationController
protect_from_forgery :except => 这个:index #处理除了index文件验证
skip_before_filter :verify_authenticity_token #这句话的作用是跳过CSRF保护,不建议这种方式处理
end
当然如果你认为你的action确实不需要验证,那可以这么写:(在application.rb中写)
#除了index,controller里的其他action都需要验证
protect_from_forgery :except => :index
protect_from_forgery :only => :index
方法(2):
修改配置文件config\environments\development.rb (这是选择开发者模式的时候,如果其他模式,则在相应的文件下,修改即可)
代码
# Disable request forgery protection in development environment
config.action_controller.allow_forgery_protection = false
然后重启服务器
方法(3)
在jquery或form中加入
Ruby代码
<%= tag(:input, :type => "hidden", :name =>
request_forgery_protection_token.to_s, :value =>
form_authenticity_token) %>