Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 903
tpower Avatar asked Aug 04 '10 20:08

tpower


People also ask

What does as do in Rails routes?

The :as option creates a named path. You can then call this path in your controllers and views (e.g. redirect_to things_path ). This isn't very useful for the root path (as it already named root ), but is very useful for new routes you add.

What is RESTful routes in Rails?

In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as. map.resources :photos. creates seven different routes in your application: HTTP verb.

What is namespace in Rails routes?

This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.


1 Answers

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.

like image 67
nfm Avatar answered Sep 28 '22 07:09

nfm