How to force Rails to consider a param with a dot in the value like google.com
(e.g. /some_action/google.com
) a single param and not "id" => "google", "format"=> "com"
?
The parameter value should be "id" => "google.com"
By default, dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. However, you can add some regex requirements to the route parameters. Here, you want to allow the dots in the parameters.
match 'some_action/:id' => 'controller#action', :constraints => { :id => /[0-z\.]+/ }
And in rails 2.3:
map.connect 'some_action/:id', :controller => 'controller', :action => 'action', :requirements => { :id => /[0-z\.]+/ }
Relevent rails guides section
In Rails 4 I used:
get 'operation/:p1/:p2', to: 'operation#get', constraints: { p1: /[^\/]+/, p2: /[^\/]+/ }
it allows any character in both params (other than '/')
And when used with the resources notation, it can be done like this:
resources :post,
only: [ :create, :index, :destroy ],
constraints: { id: /[0-z\.]+/ }
Tested in Rails 4.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With