16

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.

4 Answers 4

23

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 => /.*/
2
  • 3
    That doesn't account for if you have another / in the url, such as /search/hello+world/page/20. Might be better to go with everything but a / with /[^/]+/
    – Tallboy
    Commented Nov 11, 2013 at 23:23
  • Worked great in Rails 4.2 Commented Dec 28, 2018 at 0:16
9

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 => /[^\/]+/ }

2
  • 1
    I like your answer best, although I think that should be /[^\/]+/
    – Tallboy
    Commented Nov 11, 2013 at 23:26
  • Thanks, I actually typed the backslash, but it wasn't showing up on the page. I had to type 2 backslashes to show 1. Commented Nov 12, 2013 at 15:38
3

The dot is not allowed by default in Rails routing because the dot is considered the extension of the page. You should avoid using dots in URLs.

However, in your case you can instruct Rails to consider the dot for the :user_name parameter passing a regular expression.

map.connect 'developer/:user_name/list', :controller => "developers", :action => "list", :method=> :get, :user_name => /[\w.]+/

PS. Because of map.connect, you are using a very old version of Rails (Rails < 3). You should upgrade your application.

0
0

Are you doing a DELETE request to the delete url? Note that the route is defined with :method=>:delete, so it expects DELETE request (not GET).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.