为什么 Rails 重定向我的 POST 而不是 GET? [英] Why is Rails redirecting my POST but not GET?

查看:58
本文介绍了为什么 Rails 重定向我的 POST 而不是 GET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据发布到某个 controller#action 对,但我的应用程序在 POST(但不是 GET)上重定向我,我不知道为什么.

I'm trying to post data to a certain controller#action pair, but my app redirects me on POST (but not GET), and I can't figure out why.

我用一种方法构建了一个简单的控制器:

I built a bare-bones controller with one method:

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

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

My routing file has only one rule:

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

这是我 GET 和 POST 时的结果:

Here's my result when I GET and POST, though:

$ 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>

规格:rails 2.3.8,ruby 1.8.7

推荐答案

关闭protect_from_forgery.

注释掉(或删除)ApplicationController 中的protect_from_forgery.

Commenting out (or delete) protect_from_forgery in ApplicationController.

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

对于一个或多个控制器

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

For one or more controllers

Add skip_before_filter :verify_authenticity_token to the controller declaration.

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

对于一项或多项操作

为上述 skip_before_filterprotect_from_forgery 命令添加一个 :except 选项.

For one or more actions

Add an :except option to the foregoing skip_before_filter or protect_from_forgery commands.

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

class MyOtherController < ApplicationController
    skip_before_filter :verify_authenticity_token, :except => [:create]
end

这篇关于为什么 Rails 重定向我的 POST 而不是 GET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆