我有一个Rails应用程序,为iPhone应用程序提供一些API。我希望能够简单地在资源上发布而不必担心获取正确的CSRF令牌。我尝试了一些在stackoverflow中看到的方法,但似乎它们在Rails 3中不再起作用。
谢谢你的帮助。
谢谢你的帮助。
在您想要禁用CSRF的控制器中,请检查:
skip_before_action :verify_authenticity_token
或者仅在少数方法之外禁用它:
skip_before_action :verify_authenticity_token, :except => [:update, :create]
或者只禁用特定方法:
skip_before_action :verify_authenticity_token, :only => [:custom_auth, :update]
更多信息:RoR请求伪造保护
Rails 3中,您可以在控制器中禁用特定方法的CSRF令牌:
protect_from_forgery :except => :create
ApplicationController
中。Mike Lewis 在下面的回复中提到的 skip_before_filter :verify_authenticity_token
是如何在每个控制器上禁用它的,假设该控制器继承自 ApplicationController
。 - NudeCanalTroll在Rails 4中,您现在可以选择使用skip_before_action
而不是skip_before_filter
。
# Works in Rails 4 and 5
skip_before_action :verify_authenticity_token
或者# Works in Rails 3 and 4 (deprecated in Rails 4 and removed in Rails 5)
skip_before_filter :verify_authenticity_token