Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode (.) Dot in url Rails

I am having routes like below to delete/list an user.

map.connect 'developer/:user_name/delete',:controller=>"developers",:action=>"delete",:method=>:delete  

map.connect 'developer/:user_name/list',:controller=>"developers",:action=>"list",:method=>:get

While listing the user by encoding the Dot with %2E, i can see the success response

http://localhost:3000/developer/testuser%2Ehu/list

But While trying to delete the user who containing the Dot(.), throws 404 error.

http://localhost:3000/developer/testuser%2Ehu/delete, how to fix this issue.
like image 206
loganathan Avatar asked Jan 20 '12 12:01

loganathan


2 Answers

Avdi Grimm wrote on this subject: http://avdi.org/devblog/2010/06/18/rails-3-resource-routes-with-dots-or-how-to-make-a-ruby-developer-go-a-little-bit-insane/

You'll want to do something like this (full credit to avdi)

  resources :users, :constraints => { :id => /.*/ } do
    resources :projects
  end

A commenter on the post says you can also do:

resources :users, :id => /.*/
like image 191
Jesse Wolgamott Avatar answered Oct 18 '22 03:10

Jesse Wolgamott


I just had a similar problem, my search page url is /search/search_term. When search_term had a dot, Rails interpreted it as the request format. If I tried to search for book.html, it actually searched for book, because Rails interpreted html as the format. An unrecognized format would return an error.

In my case, the first solution from Avdi Grimm didn't work, because my search is paginated, and the page number also goes in the url (/search/book/2). The solution for me was accepting everything but a slash for the search_term (the last solution from Avdi Grimm's post):

resources :users, :constraints => { :id => /[^\/]+/ }

like image 9
Guilherme Garnier Avatar answered Oct 18 '22 03:10

Guilherme Garnier