问题
I have the following route defined:
map.resources :images, :only => [ :index, :new, :destroy ]
when I do a rake routes
I get the following:
image DELETE /images/:id(.:format) {:action=>"destroy", :controller=>"images"}
My problem is, I would like to use file names as my :id
including any extension. At the moment my ids are getting to the controller minus the extension. Is there any way I can customize the above map.resources to generate the following path:
image DELETE /images/:id {:action=>"destroy", :controller=>"images"}
i.e. not have the extension used as :format
?
回答1:
The .
character is defined in ActionController::Routing::SEPARATORS
, which lists special characters to split the URL on.
If you want to avoid splitting the URL at .
s, you need to pass a :constraints => { :id => /regexp/ }
argument to map.resources
.
See my related question and answer for more info.
回答2:
I couldn't figure out how to pass the id intact to the controller but this is the work around I used to reconstruct the id:
id = [ params['id'], params['format'] ].compact.join '.'
来源:https://stackoverflow.com/questions/3409395/how-can-i-get-a-rails-route-to-keep-the-extension-as-part-of-the-id