# form\_for with RESTful route calling the wrong action

**URL:** <https://discuss.rubyonrails.org/t/form-for-with-restful-route-calling-the-wrong-action/35134>\
**Category:** rubyonrails-talk\
**Created:** [October 7, 2008, 8:10pm UTC](https://discuss.rubyonrails.org/t/form-for-with-restful-route-calling-the-wrong-action/35134 "2008-10-07T20:10:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Schulte](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@Eric\_Schulte](https://discuss.rubyonrails.org/u/Eric_Schulte)\
**Post date:** [October 7, 2008, 8:10pm UTC](https://discuss.rubyonrails.org/t/form-for-with-restful-route-calling-the-wrong-action/35134/1 "2008-10-07T20:10:51Z")

</div>

Hi,

I am using RESTFUL routes with the following setup with a form\_for tag, but instead of calling the :create action my form is calling the :new action. I have read and re-read the resources documentation, and I am confident that I have the right combination of paths, and posting methods, please take a look at this setup and let me know if I'm doing something stupid or if this is an error w/rails routing.

,----[environment.rb]

> # Specifies gem version of Rails to use when vendor/rails is not present RAILS\_GEM\_VERSION = '2.1.0' unless defined? RAILS\_GEM\_VERSION

`----

,----[processing\_service\_levels/new.html.erb]

> &nbsp;&nbsp;&nbsp;&nbsp;\<% form\_for(:processing\_service\_levels, @level, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:url =\> processing\_service\_level\_path(@level), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:html =\> { :method =\> :post}) do |f| %\>

`----

,----[routes.rb]

> &nbsp;&nbsp;map.resources :processing\_service\_levels

`----

,----[processing\_service\_levels\_controller.rb]

> &nbsp;&nbsp;# POST /processing\_service\_levels &nbsp;&nbsp;# POST /processing\_service\_levels.xml &nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@level = ProcessingServiceLevel.new(params[:id]) &nbsp;&nbsp;&nbsp;&nbsp;@level.process\_hours = (Integer(params[:months]) \* 30 \* 24) + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Integer(params[:days]) \* 24) + Integer(params[:hours]) &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @level.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'Service level was successfully created.' ...

`----

,----[rendered html]

> &nbsp;&nbsp;&nbsp;&nbsp;\<form action="/processing\_service\_levels/" method="post"\> &nbsp;&nbsp;&nbsp;&nbsp;... form stuff ... &nbsp;&nbsp;&nbsp;&nbsp;\<input name="commit" type="submit" value="Create" /\>

`----

I've restarted with web-server since adding the map.resources line to my routes. I've tried specifying the path using the old style

:url =\> {:controller =\> :processing\_service\_levels, :action =\> :create, :id =\> @level}

but with no success. I've googled, and searched the archives of this list, but I have had no success in finding out what's going wrong.

Please help me out!

Thanks -- Eric

---

<div class="post-metadata">

**Author:** ![Jeff1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jeff1/32/1170_2.png) [@Jeff1](https://discuss.rubyonrails.org/u/Jeff1)\
**Post date:** [October 7, 2008, 8:29pm UTC](https://discuss.rubyonrails.org/t/form-for-with-restful-route-calling-the-wrong-action/35134/2 "2008-10-07T20:29:36Z")

</div>

inline...

> Hi,
> 
> I am using RESTFUL routes with the following setup with a form\_for tag, but instead of calling the :create action my form is calling the :new action. I have read and re-read the resources documentation, and I am confident that I have the right combination of paths, and posting methods, please take a look at this setup and let me know if I'm doing something stupid or if this is an error w/rails routing.
> 
> ,----[environment.rb] \> # Specifies gem version of Rails to use when vendor/rails is not present \> RAILS\_GEM\_VERSION = '2.1.0' unless defined? RAILS\_GEM\_VERSION `----

What code do you have in your controller? It should be something like this:

class ProcessingServiceLevelsController \< ApplicationController

&nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@processing\_service\_level = ProcessingServiceLevel.new &nbsp;&nbsp;end

end

> ,----[processing\_service\_levels/new.html.erb] \> \<% form\_for(:processing\_service\_levels, @level, \> :url =\> processing\_service\_level\_path(@level), \> :html =\> { :method =\> :post}) do |f| %\> `----

Given the above controller code, you can use this instead:

\<% form\_for @processing\_service\_level do |f| %\>

That should create a POST action to /processing\_service\_levels, which should call your create action.

Last resort, try restarting your web server (mongrel or webrick or whatever).

Jeff

purpleworkshops.coom

---

<div class="post-metadata">

**Author:** ![Eric\_Schulte](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@Eric\_Schulte](https://discuss.rubyonrails.org/u/Eric_Schulte)\
**Post date:** [October 7, 2008, 8:47pm UTC](https://discuss.rubyonrails.org/t/form-for-with-restful-route-calling-the-wrong-action/35134/3 "2008-10-07T20:47:32Z")

</div>

inline...

Jeff \<cohen.jeff@gmail.com\> writes:

> inline...
> 
> > Hi,
> > 
> > I am using RESTFUL routes with the following setup with a form\_for tag, but instead of calling the :create action my form is calling the :new action. I have read and re-read the resources documentation, and I am confident that I have the right combination of paths, and posting methods, please take a look at this setup and let me know if I'm doing something stupid or if this is an error w/rails routing.
> > 
> > ,----[environment.rb] \> # Specifies gem version of Rails to use when vendor/rails is not present \> RAILS\_GEM\_VERSION = '2.1.0' unless defined? RAILS\_GEM\_VERSION `----
> 
> What code do you have in your controller? It should be something like this:
> 
> class ProcessingServiceLevelsController \< ApplicationController
> 
> &nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@processing\_service\_level = ProcessingServiceLevel.new &nbsp;&nbsp;end
> 
> end

the relevant methods in my processing\_service\_levels\_controller.rb are

&nbsp;&nbsp;# GET /processing\_service\_levels/new &nbsp;&nbsp;# GET /processing\_service\_levels/new.xml &nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@processing\_service\_level = ProcessingServiceLevel.new &nbsp;&nbsp;&nbsp;&nbsp;@version = ProcessingServiceVersion.find(params[:version]) &nbsp;&nbsp;&nbsp;&nbsp;@parent = @version.processing\_service &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # new.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :json =\> @level } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;# POST /processing\_service\_levels &nbsp;&nbsp;# POST /processing\_service\_levels.xml &nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@level = ProcessingServiceLevel.new(params[:id]) &nbsp;&nbsp;&nbsp;&nbsp;@level.process\_hours = (Integer(params[:months]) \* 30 \* 24) + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Integer(params[:days]) \* 24) + Integer(params[:hours]) &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @level.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'Service level was successfully created.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { redirect\_to(:controller =\> :processing\_service\_versions, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:action =\> :show, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:id =\> @version, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:parent =\> @parent, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:level =\> @level) } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :json =\> @service, :status =\> :created, :location =\> @level } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { render(:action =\> "new", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:version =\> @level.processing\_service\_version, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:parent =\> @level.processing\_service\_version.processing\_service) } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :json =\> @level.errors, :status =\> :unprocessable\_entity } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

not that the create method has ever successfully been called 😉

> > ,----[processing\_service\_levels/new.html.erb] \> \<% form\_for(:processing\_service\_levels, @level, \> :url =\> processing\_service\_level\_path(@level), \> :html =\> { :method =\> :post}) do |f| %\> `----
> 
> Given the above controller code, you can use this instead:
> 
> \<% form\_for @processing\_service\_level do |f| %\>

I switched to the form\_for argument you suggested, which renders to the following html

&nbsp;&nbsp;\<form action="/processing\_service\_levels" class="new\_processing\_service\_level" id="new\_processing\_service\_level" method="post"\>

> \> That should create a POST action to /processing\_service\_levels, which should call your create action.

Is there a way to inspect which method (put, post, get etc...) is being sent?

When I submit this form I am still directed to the processing\_service\_controllers/new action. I'm really not clear on what's going on, and why this one particular model has issues when all of my other models are working fine. Also the processing\_service\_levels/edit form is submitting to the edit actions instead of update.

> Last resort, try restarting your web server (mongrel or webrick or whatever).

I've been restarting my web-server after almost every change.

Thanks for the advice, if you have any other ideas please let me know -- Eric

---

<div class="post-metadata">

**Author:** ![Jeff1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jeff1/32/1170_2.png) [@Jeff1](https://discuss.rubyonrails.org/u/Jeff1)\
**Post date:** [October 7, 2008, 8:58pm UTC](https://discuss.rubyonrails.org/t/form-for-with-restful-route-calling-the-wrong-action/35134/4 "2008-10-07T20:58:45Z")

</div>

Are you sure it's just not being redirected? If the .save fails inside the create() action, it looks like the code will render the new form again - so perhaps it's been misleading?

Can you check the development.log, and see if the #create action is really ever getting called? At the end of the "Processing..." line in the log, you should see "[POST]" to confirm that the POST method is being used.

Sorry if you've already checked all this... just trying to think of what to try next.

Jeff

---

<div class="post-metadata">

**Author:** ![Eric\_Schulte](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@Eric\_Schulte](https://discuss.rubyonrails.org/u/Eric_Schulte)\
**Post date:** [October 7, 2008, 9:05pm UTC](https://discuss.rubyonrails.org/t/form-for-with-restful-route-calling-the-wrong-action/35134/5 "2008-10-07T21:05:22Z")

</div>

Jeff \<cohen.jeff@gmail.com\> writes:

> > Is there a way to inspect which method (put, post, get etc...) is being sent?
> > 
> > When I submit this form I am still directed to the processing\_service\_controllers/new action. I'm really not clear on what's going on, and why this one particular model has issues when all of my other models are working fine. Also the processing\_service\_levels/edit form is submitting to the edit actions instead of update.
> 
> Are you sure it's just not being redirected? If the .save fails inside the create() action, it looks like the code will render the new form again - so perhaps it's been misleading?
> 
> Can you check the development.log, and see if the #create action is really ever getting called? At the end of the "Processing..." line in the log, you should see "[POST]" to confirm that the POST method is being used.

Thank for the help Jeff,

I checked the logs, and immediately after rendering the processing\_service\_levels/new page, the next line is

Processing ProcessingServiceLevelsController#new (for 127.0.0.1 at 2008-10-07 13:55:32) [GET]

for more of the log see [1] below

which leads me to believe that the form is submitting directly to the new action using GET. I don't see how this could be the case given the raw html of the "new" page, unless maybe something is twisted inside of the rails routing...

I've also tried sticking error raising code in the from of the udpate action, but said code is never reached...

Thanks -- Eric

> Sorry if you've already checked all this... just trying to think of what to try next.
> 
> Jeff

[1] ,----[development log]

> Processing ProcessingServiceLevelsController#new (for 127.0.0.1 at 2008-10-07 13:55:25) [GET] &nbsp;&nbsp;Session ID: f69ec3fdb850969c52d3e6173df8e72e &nbsp;&nbsp;Parameters: {"action"=\>"new", "version"=\>"20", "controller"=\>"processing\_service\_levels"} &nbsp;&nbsp;User Columns (0.9ms) SHOW FIELDS FROM `users` &nbsp;&nbsp;User Load (0.1ms) SELECT \* FROM `users` WHERE (`users`.`id` = 5) LIMIT 1 &nbsp;&nbsp;ProcessingServiceLevel Columns (0.9ms) SHOW FIELDS FROM `processing_service_levels` &nbsp;&nbsp;ProcessingServiceVersion Columns (0.8ms) SHOW FIELDS FROM `processing_service_versions` &nbsp;&nbsp;ProcessingServiceVersion Load (0.1ms) SELECT \* FROM `processing_service_versions` WHERE (`processing_service_versions`.`id` = 20) &nbsp;&nbsp;ProcessingService Columns (0.8ms) SHOW FIELDS FROM `processing_services` &nbsp;&nbsp;ProcessingService Load (0.1ms) SELECT \* FROM `processing_services` WHERE (`processing_services`.`id` = 3) Rendering template within layouts/application Rendering processing\_service\_levels/new Rendered processing\_services/\_processing\_service (3.7ms) Rendered processing\_service\_versions/\_processing\_service\_version (3.7ms) Rendered processing\_service\_levels/\_form (4.0ms) Rendered users/\_user\_bar (3.3ms) Completed in 130ms (View: 35, DB: 4) | 200 OK [[http://localhost/processing\_service\_levels/new?version=20](http://localhost/processing_service_levels/new?version=20)]
> 
> Processing ProcessingServiceLevelsController#new (for 127.0.0.1 at 2008-10-07 13:55:32) [GET] &nbsp;&nbsp;Session ID: f69ec3fdb850969c52d3e6173df8e72e &nbsp;&nbsp;Parameters: {"month"=\>"0", "commit"=\>"Create", "processing\_service\_level"=\>{"name"=\>"Nothing", "description"=\>"Nothing", "processing\_service\_version\_id"=\>"20"}, "hour"=\>"0", "authenticity\_token"=\>\*\*\*, "action"=\>"new", "day"=\>"0", "controller"=\>"processing\_service\_levels"} &nbsp;&nbsp;User Columns (1.1ms) SHOW FIELDS FROM `users` &nbsp;&nbsp;User Load (0.1ms) SELECT \* FROM `users` WHERE (`users`.`id` = 5) LIMIT 1 &nbsp;&nbsp;ProcessingServiceLevel Columns (1.1ms) SHOW FIELDS FROM `processing_service_levels`

`----

---

<div class="post-metadata">

**Author:** ![Pardee\_Roy](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Pardee\_Roy](https://discuss.rubyonrails.org/u/Pardee_Roy)\
**Post date:** [October 7, 2008, 9:09pm UTC](https://discuss.rubyonrails.org/t/form-for-with-restful-route-calling-the-wrong-action/35134/6 "2008-10-07T21:09:26Z")

</div>

Could your browser cache be screwing with you maybe? Any difference if you switch to a different browser?

---

<div class="post-metadata">

**Author:** ![Eric\_Schulte](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@Eric\_Schulte](https://discuss.rubyonrails.org/u/Eric_Schulte)\
**Post date:** [October 7, 2008, 9:10pm UTC](https://discuss.rubyonrails.org/t/form-for-with-restful-route-calling-the-wrong-action/35134/7 "2008-10-07T21:10:21Z")

</div>

My bad,

it looks like the creation form was nested inside of empty \<form\>\</form\> tags. These surrounding tags, must have been what was breaking the submit action to GET rather than POST. I removed these tags and it appears to be working.

Thanks for the help! Sorry about the noise -- Eric

"Eric Schulte" \<schulte.eric@gmail.com\> writes:
