开发者

Trying to create a POST request but getting No route matches [GET]

I'm trying to do something similar to Railscasts #255 but I'm getting a No Route error:

In Ryan's routes.rb file:

    post "versions/:id/revert" => "versions#revert", :as => "revert_version"

In in the controller where he uses the route, versions_controller.rb

    link = view_context.link_to(link_name, revert_version_path(@version.next, :redo => !params[:redo]), :method => :post)
    redirect_to :back, :notice => "Undid #{@version.event}. #{link}"

In my routes.rb

    post "/approve/:id" => "listings#approve", :as => "li开发者_开发百科sting_approve"

and view where I use my link:

    <%= link_to 'Approve Content', listing_approve_path(@listing), :method => :post %>

My tests return to me a ActionController::RoutingError: No route matches [GET] "/approve/1"

If I leave the method as a GET everything works.. Using rails 3.1.0rc5. Any guidance as to what I'm doing wrong here would be very much appreciated..

EDIT: routes.rb file (the last line is set as match right now to work)

RLR::Application.routes.draw do

  root :to => "home#index"

  devise_for :users, :controllers => { :registrations => "registrations" }
  devise_for :users

  match '/user' => "layouts#index", :as => :user_root

  resources :users, :only => :show
  resources :layouts, :only => [:index, :show]
  resources :listings
  resources :features
  resources :orders

  match "/preview/:id" => "listings#preview", :as => "listing_preview", :via => "get"
  match "/approve/:id" => "listings#approve", :as => "listing_approve"

end


Hmmmm, it looks right to my eye. The test sounds like it is generating a GET instead of a POST though, so it might be a problem with the link_to call. You've got :method => :post there, so it should be fine. http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to seems to indicate that link_to will generate some javascript to make a POST call on click (and that users with javascript disabled will get a normal GET link unless you use :href="#"), so it might be because your test engine isn't running the javascript.

You can fix this by changing it to a button that submits a hidden form, but that might not be the visual representation you want.


It might be a precedence thing - the first matching route definition in routes.rb is used, so if you have a resources route or something like that it may be matching on that first.


I got the same problem in my rails application and I solved it the same way you did by doing a via: :get on the match instead of a via: :post. I think for some reason when you send a request in the format of /something/:id it will automatically assume its a [GET] request and search for a get route. This of course will cause problems in your routes if you have it as a :POST.

If anyone has a better solution or idea as to why you cannot send a post request in the format '/something/:id' let me know please.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜