Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Rails 3 Route is Failing - No route matches

In my config/routes.rb I have:

  post "portal_plan_document/update"

rake routes confirms this:

$ rake routes
portal_plan_document_update POST /portal_plan_document/update(.:format) {:controller=>"portal_plan_document", :action=>"update"}
....

In my code I have:

<%= form_for @plan_doc, 
    :url => portal_plan_document_update_path, 
    :method => "POST", :remote => true do |f| %>

In my log file I see:

Started POST "/portal_plan_document/update" for 127.0.0.1 at 2011-03-31 18:04:37 -0400

ActionController::RoutingError (No route matches "/portal_plan_document/update"):

I am lost as what to do from here. Any help would be greatly appreciated!

I should state I am using Ruby 1.9.2 and Rails 3.0.5. Oh, I have restarted the server (WebBrick w/rails server) after updating routes.rb.

Jeremy

like image 351
jeremy Avatar asked Mar 31 '11 22:03

jeremy


1 Answers

Figured it out! :) if you have a non-empty object, rails assumes you want to update that object. i.e., use a 'PUT' instead of a 'POST'

to accomplish 'PUTs', rails will put a hidden input in the form, with "_method" = "put". so, it LOOKS like it's a POST, but rails is treating it as a PUT.

if you actually want to just update an object (what it looks like you're doing), a PUT is better, and you should just switch your routes over to PUT.

if (like I was), you're doing something that really requires a POST (i.e., it can't be sent more than once safely), you can write your form_for like this:

<%= form_for @plan_doc, 
:url => portal_plan_document_update_path, 
:html=>{:method => "POST"}, :remote => true do |f| %>

to confirm, look at the generated HTML source, and make sure the hidden "_method" field is not set to "put"

like image 134
YenTheFirst Avatar answered Oct 24 '22 00:10

YenTheFirst