Posted over 3 years ago. Visible to the public.

Rails: How to get URL params without routing parameters (or vice versa)

Rails' params hash contains any request parameters (URL parameters or request payload) as well as routing parameters like :controller, :action, or :id.

To access only URL parameters, use request.query_parameters. Routing params are available through request.path_parameters.

Copy
# On /users?query=Bob&page=2 >> request.params => {"page"=>"2", "query"=>"Bob", "controller"=>"users", "action"=>"index"} >> request.query_parameters => {"page"=>"2", "query"=>"Bob"} >> request.path_parameters => {:controller=>"users", :action=>"index"}

Note that path_parameters is a Hash with Symbol keys, not a HashWithIndifferentAccess like the other.

Your development team has a full backlog of feature requests, chores and refactoring coupled with deadlines? We are familiar with that. With our "DevOps as a Service" offering, we support developer teams with infrastructure and operations expertise.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
over 3 years ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more