Additional Rails 3 Routing

I have to configure my API with versions like this, with just a little tweak for backward compatibility. On my routes, I:

scope '(api(/:version))', :module => :api, :version => /v\d+?/ do … scope '(categories/:category_id)', :category_id => /\d+/ do … resources :sounds … end end 

with the successful goal already achieved in that the following URL will reach the same place

 /api/v1/categories/1/sounds/2 /api/categories/1/sounds/2 /categories/1/sounds/2 /sounds/2 

My directory structure is this:

enter image description here

The problem that I see in my generations of links is in my views. For example, on the sound show page, I have button_to to delete the sound

 <%= button_to 'Delete', @sound, :confirm => 'Are you sure?', :method => :delete %> 

Which generates the following URL in the form of action :

 "/api/sounds/1371?version=1371" 

In addition, the delete method does not work, instead it is sent as a POST

The interesting parts of rake routes are:

  sounds GET (/api(/:version))(/categories/:category_id)/sounds(.:format) {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"index", :category_id=>/\d+/} POST (/api(/:version))(/categories/:category_id)/sounds(.:format) {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"create", :category_id=>/\d+/} new_sound GET (/api(/:version))(/categories/:category_id)/sounds/new(.:format) {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"new", :category_id=>/\d+/} edit_sound GET (/api(/:version))(/categories/:category_id)/sounds/:id/edit(.:format) {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"edit", :category_id=>/\d+/} sound GET (/api(/:version))(/categories/:category_id)/sounds/:id(.:format) {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"show", :category_id=>/\d+/} PUT (/api(/:version))(/categories/:category_id)/sounds/:id(.:format) {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"update", :category_id=>/\d+/} DELETE (/api(/:version))(/categories/:category_id)/sounds/:id(.:format) {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"destroy", :category_id=>/\d+/} 

and server logs show:

 Started POST "/api/sounds/1371?version=1371" for 127.0.0.1 at Fri May 06 23:28:27 -0400 2011 Processing by Api::SoundsController#show as HTML Parameters: {"authenticity_token"=>"W+QlCKjONG5i/buIgLqsrm3IHi5gdQVzFGYGREpmWYs=", "id"=>"1371", "version"=>371} 

I use jQuery as my UJS and have the latest rails.js version for jQuery:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> <!-- must be >= 1.4.4 for the rails 3 link js to workβ€”> <script src="/javascripts/jquery-ujs/src/rails.js?1304736923" type="text/javascript"></script> 






I know this is tl;dr , but my question is: " What do I need to put button_to link_to and other presentation tags in my link so that the URL helpers generate the correct path for my installation? " I have tried almost every a combination that I can think of, but cannot make them be in the right place.

+2
source share
1 answer

It turns out the solution was simple. I had two errors here that caused the wrong generation path, and the url for adding parameters.

  • over the route resources :sounds , I mistakenly had this line:

     match '/sounds/:id(/:format)' => 'sounds#show' 

    I have this line so that sounds/123/xml can be cached by the page. This caused all the routing to show , and I realized that the error was that I had :format in parens, and the match should be get . Now he reads:

     get '/sounds/:id/:format' => 'sounds#show' 
  • Next, in the button_to link, I put the @sound object as my second parameter. Rails tried to be an intelligent extrapolation of the correct URL, but did not execute the optional api :version parameter. Change it to

     sound_path(:id => @sound.id) 

    worked like a charm.

+2
source

Source: https://habr.com/ru/post/1393580/


All Articles