Request.post? returns false on POST request

def test
if request.post?
render :text => “post”
else
render :text => “other”
end
end

when i post with a form to this page - goes into else branch, when
trying to output request.post? - it’s false

On Wed, Aug 17, 2011 at 8:42 AM, art [email protected] wrote:

when i post with a form to this page - goes into else branch, when
trying to output request.post? - it’s false

Are you sure you’re not actually doing a PUT via hidden form fields?


Hassan S. ------------------------ [email protected]

twitter: @hassan

It works fine for me. Here is my code:

Pages#home

Find me in app/views/pages/home.html.erb

Submit

===

Test2App::Application.routes.draw do

root :to => “pages#home”

post ‘pages/test’

===

class PagesController < ApplicationController
def test
if request.post?
render :text => “post”
else
render :text => “other”
end
end
end

When I click the submit button, ‘post’ appears in the new page that
loads in my browser.

If I change the method of the form to ‘get’, and reload the home page,
then click on the Submit button, I get a Routing Error:

No route matches “/pages/test”

Then if I add:

get ‘pages/test’

to my routes.rb file, reload the home page, then click on the Submit
button, I get:

other

on the new page that loads in my browser.

Hassan S. wrote in post #1017170:

On Wed, Aug 17, 2011 at 8:42 AM, art [email protected] wrote:

when i post with a form to this page - goes into else branch, when
trying to output request.post? - it’s false

Are you sure you’re not actually doing a PUT via hidden form fields?

Try this:

def test

render :text => request.method

end

render :text => request.method # --> ‘POST’

and yet
render :text => request.post? # --> false

rails 3.0.9

Yes, it is a put via hidden field,

but how did a hidden field got there in a first place??

On Aug 17, 9:00pm, Hassan S. [email protected]

art wrote in post #1017184:

Yes, it is a put via hidden field,

but how did a hidden field got there in a first place??

Post the code that creates your form.

art wrote in post #1017180:

render :text => request.method # --> ‘POST’

and yet
render :text => request.post? # --> false

rails 3.0.9

Okay. Web browser can’t really send PUT or DELETE requests. So rails
fakes it by putting a hidden form field on the page with a value of
‘put’ or ‘delete’. Then rails checks for that form field. I guess
request.method returns the actual type of the request that the browser
sent–not the type that rails pretends was sent.