Jellicle
Jellicle

Reputation: 30226

Why is Rails redirecting my POST but not 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'

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>

specs: rails 2.3.8, ruby 1.8.7

Upvotes: 2

Views: 262

Answers (1)

Jellicle
Jellicle

Reputation: 30226

Turn off protect_from_forgery.

For all controllers

Commenting out (or delete) protect_from_forgery in ApplicationController.

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

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

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

Upvotes: 2

Related Questions