How can I get a rails route to keep the extension as part of the id?

别来无恙 提交于 2021-02-18 22:04:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!