rails protect_from_forgery 引发异常

rails protect_from_forgery raises with exception

我有 Rails 项目正在制作中。 但在本地主机上,它会在每个 POST 请求 ActionController::InvalidAuthenticityToken 时引发。我知道那是什么。 AuthToken 可以防止 csrf 攻击。我的 application_controller.rb

protect_from_forgery with: :exception #this line raise exception

csrf_meta_tags 出席。 我在其他 Rails 项目中没有任何问题。

如果我删除参数 with: :exception 会话将在重新加载页面后重置。

有什么问题?

你能试试这个吗

protect_from_forgery with: :null_session

问题已解决!

项目在 config/environments/development.rb

中有配置

Rails.application.routes.default_url_options[:host] = 'my_project.dev:3000'

我试图在 localhost:3000 上加载项目,但是 url 是 my_project.dev:3000