开发者

Simple Rails 3 Route is Failing - No route matches

开发者 https://www.devze.com 2023-02-21 11:02 出处:网络
In my config/routes.rb I have: post \"portal_plan_document/update\" rake routes confirms this: $ rake routes

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 s开发者_C百科tate 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


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"


Try using that instead please :

:method => :post

If this does not still work, please lose the remote attribute and give it a try. Does it work without it ?


I had the same problem while upgrading a simple app from Rails 2 to Rails 3. As you may guess I was updating all "remote_form_for(@item) (..)" helpers to "form_for :item remote => true (..)" syntax.

In my case this code from a items/_new.html.erb partial:

<%= form_for :item, :remote => true do |f| %>
<!--FIELDS-->
<% end %>

Gave me this error:

Started POST "/items/new" for 127.0.0.1 at Fri Aug 12 18:19:23 +0200 2011

ActionController::RoutingError (No route matches "/items/new")

As you can notice the method was a correct "POST", not a "PUT". The problem lied in the routing... I don't know why but when a remote POST method is sent by a partial, Rails routes the POST request to "/items/new" instead of "/items" route. Even if the purpose is to create a new "item" so the POST request should be correctly (and RESTfully) routed to "/items".

This code, with explicit action and controller, solved the problem:

<%= form_for :item, :remote => true, :url => { :controller => "items", :action => "create" } do |f| %>
<!--FIELDS-->
<% end %>
0

精彩评论

暂无评论...
验证码 换一张
取 消