This project is archived and is in readonly mode.
Routing when member name is update
-
lic (at crd)
So far I have fixed with a workaround
namespace :api do resources :profiles, :only => [:show, :create] end match '/api/profiles/:id/update(.:format)' => 'profile#update', :method => :postbut shouldn't it work as in the Rails2 API ? I mean with a proper syntax?
-
Andrew White
- State changed from new to wontfix
- Importance changed from to Low
Browsers don't support the PUT and DELETE http methods either so to work around this problem you can pass a form parameter which overrides the HTTP method. Just pass _method=put and the normal route will be recognised. Alternatively, if you can't change the Flash movie then just add the update route manually as you have done.
-
lic (at crd)
Sorry Andrew but I can't pass a _method=put. My payload is XML or JSON.
-
Andrew White
Okay, how about sending a custom header? You can set the X-HTTP-Method-Override header to 'PUT' and the Rack middleware will adjust the HTTP method. You'll need to provide a cross-domain policy file on the server or the request will fail: http://kb2.adobe.com/cps/403/kb403185.html
-
lic (at crd)
Yeps that is a possibility. We can also do this in the route
namespace :api do resources :profiles, :only => [:show, :create] match '/profiles/:id/update(.:format)' => 'profiles#update', :method => :post match '/profiles/:id/delete(.:format)' => 'profiles#destroy', :method => :delete endAnd it works. My question here is why can't we make an API that is back compatible with Rails2.
-
Andrew White
How about this:
namespace :api do resources :profiles, :only => [:show, :create] do member do post :update, :path => 'update', :as => :update post :destroy, :path => 'destroy', :as => :destroy end end endYou need the :path and :as options to override the inbuilt defaults of blank for the action path and helper prefix.
-
lic (at crd)
Superb!
I didn't know that the :path option could override inbuilt defaults!
That's perfect!Thank you for helping out! I own you a beer!
-
lic (at crd)
Weird thing...
rake routes | grep profile api_profile_sessions POST /api/profile_sessions(.:format) {:action=>"create", :controller=>"api/profile_sessions"}update_api_profile POST /api/profiles/:id/update(.:format) {:action=>"update", :controller=>"api/profiles"}
api_profiles POST /api/profiles(.:format) {:action=>"create", :controller=>"api/profiles"} api_profile GET /api/profiles/:id(.:format) {:action=>"show", :controller=>"api/profiles"}So I have it and works fine.
From rspec:
Failure/Error: assert_routing("/api/profiles/PUBLICKEY/update.xml", No route matches "/api/profiles/PUBLICKEY/update.xml" -
lic (at crd)
Could it be a bug in ActionDispatch::Assertions::RoutingAssertions ?
-
Andrew White
I don't think so - the following test works in a new app:
require 'test_helper' class RoutingTest < ActionDispatch::IntegrationTest test "API routing" do assert_routing( { :path => "/api/profiles/PUBLICKEY.xml", :method => :get }, { :controller => "api/profiles", :action => "show", :id => "PUBLICKEY", :format => "xml" } ) assert_routing( { :path => "/api/profiles.xml", :method => :post }, { :controller => "api/profiles", :action => "create", :format => "xml" } ) assert_routing( { :path => "/api/profiles/PUBLICKEY/update.xml", :method => :post }, { :controller => "api/profiles", :action => "update", :id => "PUBLICKEY", :format => "xml" } ) assert_routing( { :path => "/api/profiles/PUBLICKEY/destroy.xml", :method => :post }, { :controller => "api/profiles", :action => "destroy", :id => "PUBLICKEY", :format => "xml" } ) end endAre you sure that the assert_routing call is being passed :method => :post? Are you using assert_routing in your spec directly?
-
lic (at crd)
I made it work in rspec I guess the issue was calling the wrong assertion method.
describe "Routes" do it "should route update profile from Flash" do {:post => "/api/profiles/PUBLICKEY/update.xml"}.should route_to( :controller => "api/profiles", :action => "update", :id => "PUBLICKEY", :format => "xml" ) end endOk I own you definitively a beer now!
Thank you for tips!