为什么Rails重定向我的POST而不是GET?

时间:2012-12-13 17:41:35

标签: ruby-on-rails redirect

我正在尝试将数据发布到某个controller#action对,但我的应用会在POST时重定向我(但不是GET),我无法弄清楚原因。

我用一种方法构建了一个裸骨控制器:

class NavigationsController < ApplicationController
    def foo
        render :text => 'in foo'
    end
end

我的路由文件只有一条规则:

map.connect ':controller/:action/:id'

这是我在GET和POST时的结果:

$ curl http://localhost:3000/navigations/foo/1
in foo
$ curl -d 'a=b' http://localhost:3000/navigations/foo/1
<html><body>You are being <a href="http://localhost:3000/">redirected</a>.</body></html>

specs:rails 2.3.8,ruby 1.8.7

1 个答案:

答案 0 :(得分:2)

关闭protect_from_forgery

适用于所有控制器

在ApplicationController中注释掉(或删除)protect_from_forgery

class ApplicationController < ActionController::Base
    #protect_from_forgery # See ActionController::RequestForgeryProtection for details
    # ...
end

对于一个或多个控制器

skip_before_filter :verify_authenticity_token添加到控制器声明中。

class NavsController < ApplicationController
    skip_before_filter :verify_authenticity_token
    # ...
end

对于一个或多个动作

为上述:exceptskip_before_filter命令添加protect_from_forgery选项。

class MyController < ApplicationController
    protect_from_forgery :except => :index
end

class MyOtherController < ApplicationController
    skip_before_filter :verify_authenticity_token, :except => [:create]
end
相关问题