Query Strings
A query string is indicated by the first ?
in the URL. It is used to pass additional data to the server.
http://example.com/admin?true
Rails allows you to handle query strings in a similar way to dynamic segments: by making them accessible in the params
hash.
Consider the following route:
get 'photos/:id', to: 'photos#show'
If the user agent sends an HTTP request to the photos/1?hue=10
, the params hash will contain:
params = {
id: 1,
hue: 10,
# ...
}
In the next lesson, we'll learn about adding constraints to dynamic segments.
If you have any questions or feedback, didn't understand something, or found a mistake, please leave a comment below or send me an email. You can also subscribe to my blog to receive future posts via email.