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

编程入门 行业动态 更新时间:2024-10-25 01:23:20
本文介绍了为什么 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 localhost:3000/navigations/foo/1 in foo $ curl -d 'a=b' localhost:3000/navigations/foo/1 <html><body>You are being <a href="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_filter 或 protect_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?

本文发布于:2023-10-10 22:35:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479944.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   重定向   Rails   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!