Working on a simple API, I noticed that my routing tests were failing because my :id params were strings and I was comparing them to integer values. Is there a way to automatically cast request parameters via routes.rb in some way? For example, given the following route (/profile/1), I’d like params[:id] to be an integer (1) rather than a string (“1”):
namespace :api do
namespace :v1 do
scope '/profile' do
get ':id' => 'users#show', :id => /\d+/
end
end
end
A quick example from here:
Then you’d have to do
in the controller action to cast it explicitly. I’m not sure there is another way to do this directly in routes.rb without monkey-patching how Rails collects params.
Taken from the Ruby on Rails Guides: