# Forceing PUST vs. POST in form

**URL:** <https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399>\
**Category:** Rails\
**Created:** [January 6, 2011, 7:00pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399 "2011-01-06T19:00:06Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 6, 2011, 7:00pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/1 "2011-01-06T19:00:06Z")

</div>

I am experimenting with a combined form. I wish to force the HTTP verb  
for this form to PUT. However, it always uses POST when submitted and I  
cannot determine why.

The view template code is:

\<%=form\_for( @user,  
:html =\> {  
:class =\> :edit\_user\_role,  
:id =\> :edit\_user\_role\_form,  
:method =\> :put },  
:url =\> user\_roles\_url( @user )

```
    ) do |f|-%>

```

The resulting html is:

This confuses me. The form method is not being set by the html  
attribute but a hidden field is being created instead. Since Rails  
considers only the HTTP verb for routing this simply will not work as I  
expect.

What is the problem with my approach? How does one set the form method?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 9:07pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/2 "2011-01-06T21:07:56Z")

</div>

Marnen Laibow-Koser wrote in post #972876:

> James B. wrote in post #972875:
> 
> > I am experimenting with a combined form. I wish to force the HTTP verb  
> > for this form to PUT. However, it always uses POST when submitted and I  
> > cannot determine why.
> 
> Because that’s the way Rails works. Many browsers don’t support PUT  
> forms, so Rails leaves the method as POST and adds a hidden field to  
> fake what the method “really” is. Rails does the processing of that  
> hidden field transparently, so that a POST with method “\_put” will  
> appear to the framework exactly like a real PUT.

Then does that mean one must always provide a route for the new method,  
even if it makes no sense to do so within a given context?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 7:02pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/3 "2011-01-06T19:02:56Z")

</div>

James B. wrote in post #972875:

> I am experimenting with a combined form. I wish to force the HTTP verb  
> for this form to PUT. However, it always uses POST when submitted and I  
> cannot determine why.

Because that’s the way Rails works. Many browsers don’t support PUT  
forms, so Rails leaves the method as POST and adds a hidden field to  
fake what the method “really” is. Rails does the processing of that  
hidden field transparently, so that a POST with method “\_put” will  
appear to the framework exactly like a real PUT.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 9:50pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/4 "2011-01-06T21:50:40Z")

</div>

Marnen Laibow-Koser wrote in post #972921:

> James B. wrote in post #972918:

> > Then does that mean one must always provide a route for the new method,  
> > even if it makes no sense to do so within a given context?
> 
> I don’t understand your question.

I get this error when I submit the form.

no route exists for “/users/1/roles”

However, I get to the form via “/users/1/roles/new” called from  
“/users/1/role”, which is where I want to return to after the update  
completes. At the moment I am perplexed by this, to me, mysterious  
behaviour and error message.

In other words, I can start by entering  
[http://localhost:3000/users/1/roles](http://localhost:3000/users/1/roles)

That URL displays all the roles associated with User.find(1)

From that page I follow a link to  
[http://localhost:3000/users/1/roles/new](http://localhost:3000/users/1/roles/new)  
which displays the input form. When I complete and submit the form I  
get this error:

No route matches “/users/1/roles”

which is the very URL I have just come from.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 9:18pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/5 "2011-01-06T21:18:21Z")

</div>

James B. wrote in post #972918:

> Marnen Laibow-Koser wrote in post #972876:
> 
> > James B. wrote in post #972875:
> > 
> > > I am experimenting with a combined form. I wish to force the HTTP verb  
> > > for this form to PUT. However, it always uses POST when submitted and I  
> > > cannot determine why.
> > 
> > Because that’s the way Rails works. Many browsers don’t support PUT  
> > forms, so Rails leaves the method as POST and adds a hidden field to  
> > fake what the method “really” is. Rails does the processing of that  
> > hidden field transparently, so that a POST with method “\_put” will  
> > appear to the framework exactly like a real PUT.
> 
> Then does that mean one must always provide a route for the new method,  
> even if it makes no sense to do so within a given context?

I don’t understand your question.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 9:57pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/6 "2011-01-06T21:57:31Z")

</div>

James B. wrote in post #972930:

> Marnen Laibow-Koser wrote in post #972921:
> 
> > James B. wrote in post #972918:
> 
> > > Then does that mean one must always provide a route for the new method,  
> > > even if it makes no sense to do so within a given context?
> > 
> > I don’t understand your question.
> 
> I get this error when I submit the form.
> 
> no route exists for “/users/1/roles”

Do you have a route defined with method PUT?

> However, I get to the form via “/users/1/roles/new” called from  
> “/users/1/role”,

What do you mean by that? The previous page is irrelevant – HTTP is  
stateless, remember?

So…describe exactly how you get here

> which is where I want to return to after the update  
> completes.

_Which_ page is where you want to return to?

What does the controller action look like?

> At the moment I am perplexed by this, to me, mysterious  
> behaviour and error message.
> 
> In other words, I can start by entering  
> [http://localhost:3000/users/1/roles](http://localhost:3000/users/1/roles)
> 
> That URL displays all the roles associated with User.find(1)

Right. Standard RESTful nested resources (on GET).

> From that page I follow a link to  
> [http://localhost:3000/users/1/roles/new](http://localhost:3000/users/1/roles/new)  
> which displays the input form. When I complete and submit the form I  
> get this error:
> 
> No route matches “/users/1/roles”
> 
> which is the very URL I have just come from.

But you came from it as GET, not PUT. Routes include method, not just  
URL.

What’s your rake routes output like for these paths?

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 10:13pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/7 "2011-01-06T22:13:56Z")

</div>

James B. wrote in post #972938:

> Marnen Laibow-Koser wrote in post #972933:
> 
> > But you came from it as GET, not PUT. Routes include method, not just  
> > URL.
> 
> I knew that. Nonetheless that is the bit I had confused. I was trying  
> to override the form method for the right reason but I fixed upon the  
> wrong method, PUT, instead of GET.
> 
> Thank you for the help.

You probably don’t want the form submission to be GET.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 10:07pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/8 "2011-01-06T22:07:03Z")

</div>

Marnen Laibow-Koser wrote in post #972933:

> But you came from it as GET, not PUT. Routes include method, not just  
> URL.

I knew that. Nonetheless that is the bit I had confused. I was trying  
to override the form method for the right reason but I fixed upon the  
wrong method, PUT, instead of GET.

Thank you for the help.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 10:23pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/9 "2011-01-06T22:23:33Z")

</div>

Marnen Laibow-Koser wrote in post #972943:

> You probably don’t want the form submission to be GET.

You are probably right. But for now I am simply exploring how things  
work for a nested resource. I will rewire the controller when I figure  
how what I want each bit to do and how I want it to look.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 10:27pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/10 "2011-01-06T22:27:20Z")

</div>

James B. wrote in post #972946:

> Marnen Laibow-Koser wrote in post #972943:
> 
> > You probably don’t want the form submission to be GET.
> 
> You are probably right. But for now I am simply exploring how things  
> work for a nested resource. I will rewire the controller when I figure  
> how what I want each bit to do and how I want it to look.

No! If you need to make the form submission GET, then something is  
_badly wrong_. Rails’ resource mapping should make the PUT form work by  
default. If that isn’t working, find out why. Perhaps we should take a  
look at your routes.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 11:02pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/11 "2011-01-06T23:02:06Z")

</div>

James B. wrote in post #972959:

> > No! If you need to make the form submission GET, then something is  
> > _badly wrong_. Rails’ resource mapping should make the PUT form work by  
> > default. If that isn’t working, find out why. Perhaps we should take a  
> > look at your routes.
> 
> I believe that I know the source of the problem. I have deliberately  
> created a merged edit/new form and forced the controller to map  
> different methods to specific templates. I do not intend for this to  
> remain. But I am interested in seeing what behaviour results.

It’s common to “merge” the new and edit forms. The body of the form goes  
into a partial that the two views (new and edit) share. That’s the  
cleanest and most direct way to accomplish what it sounds like you want.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 10:56pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/12 "2011-01-06T22:56:39Z")

</div>

Marnen Laibow-Koser wrote in post #972947:

> No! If you need to make the form submission GET, then something is  
> _badly wrong_. Rails’ resource mapping should make the PUT form work by  
> default. If that isn’t working, find out why. Perhaps we should take a  
> look at your routes.

I believe that I know the source of the problem. I have deliberately  
created a merged edit/new form and forced the controller to map  
different methods to specific templates. I do not intend for this to  
remain. But I am interested in seeing what behaviour results.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 11:04pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/13 "2011-01-06T23:04:43Z")

</div>

Interestingly, [w3.org](http://w3.org) indicates that there are only two valid HTTP verbs  
for the form and the submit elements. Those are GET and POST.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 6, 2011, 11:05pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/14 "2011-01-06T23:05:51Z")

</div>

James B. wrote in post #972964:

> Interestingly, [w3.org](http://w3.org) indicates that there are only two valid HTTP verbs  
> for the form and the submit elements. Those are GET and POST.

Exactly. Which is why Rails fakes PUT forms as I explained earlier.  
It’s a POST as far as the browser is concerned, but a PUT as far as  
Rails is concerned.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 7, 2011, 6:59pm UTC](https://www.ruby-forum.com/t/forceing-pust-vs-post-in-form/201399/15 "2011-01-07T18:59:02Z")

</div>

Marnen Laibow-Koser wrote in post #972966:

> James B. wrote in post #972964:
> 
> > Interestingly, [w3.org](http://w3.org) indicates that there are only two valid HTTP verbs  
> > for the form and the submit elements. Those are GET and POST.
> 
> Exactly. Which is why Rails fakes PUT forms as I explained earlier.  
> It’s a POST as far as the browser is concerned, but a PUT as far as  
> Rails is concerned.

And, if you really, really want to know how and where that happens, that  
would be here:

> **[3.3 Internal Middleware Stack - Ruby on Rails Guides: Rails on Rack](https://guides.rubyonrails.org/v2.3.8/rails_on_rack.html#internal-middleware-stack)**
>
> This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares, url maps and Rack::Builder.

Notice: Rack:MethodOveride
