June 21, 2018
Hot Topics:

Techniques to Pass and Read URL Parameters Using Rails

  • February 19, 2009
  • By Raveendran, Arumugam, Rao, and Nanjundappa
  • Send Email »
  • More Articles »


"Ruby on Rails" (RoR) has always been quite handy as far as passing and reading URL parameters are concerned. However, there is a common perception that the URL parameters get passed as a simple string. This article discusses the RoR representation of URL parameters and how users can use this information to parse and use URL parameters effectively and efficiently.

Rails and URL Parameters

First, you need to learn about parameter passing in Rails with an example. Listing 1 shows a URL with a set of parameters being sent to the recipient server that has been hosted on the local machine.

Listing 1.

   [last_name]=arumugam&arr []=1&arr []=7&arr []=3&id=15

As the users of Rails may already be aware, "users" stands for the controller class "UsersController" and "create" is the method within the "UserController" class.

   []=1&arr []=7&arr []=3&id=15"

are the parameters to the method "create".

Typically, the URL parameters are sent as a set of simple name value pairs, as in the example "name=tom&age=21". In the Rails convention, also, parameters are sent as a set of name value pairs. However, there is a slight difference. Rails eases parameter passing and makes the object relational mapping by qualifying the parameters with the model object and the model attributes. In the example above, the term "user" maps to the model (table) user and "first_name" maps to the attribute "first_name" of the model class (column first_name of the table user).


"Rails" allows the users to send an array of values as parameters as well. In Listing 1, the values "arr []=1&arr []=7&arr []=3" represent an array. The name of the array is "arr" and the values at the indices 0, 1, and 2 respectively.

Rails Parameters and HashMaps

All the URL parameters are represented internally in Rails as a set of HashMaps. This is contrary to the usual belief that URL parameters are sent as nothing but a simple string (although Rails does not inhibit users from reading the parameters as simple strings). In the example cited above, the internal mapping of the parameters as a HashMap will be as depicted in Figure 1.

Figure 1.

It can be easily seen from Figure 1 that Rails maps the main URL parameters into a simple HashMap containing a key and a value. For the parameter "id" that is passed, the value 15 is kept directly in the value section. However, for complex parameters such as "user", the value field holds the address of the HashMap where the actual values are kept. The array "arr" is also treated similarly to the parameter "user".

Rails Code and Parameter HashMap

A careful investigation of the URL parameters and the way Rails structures those parameters helps the user pass those parameters easily, as shown in the snippet in Listing 2.

Listing 2.

 1 params.each {|key,value|
 2    puts "key=>#{key}"
 3    if value.class.to_s != "String"
 4       value.each {|k,v|
 5       puts "Hash inner key=>#{k}"
 6       puts "Hash inner value=>#{v}"
 8    elseif value.class.to_s != "array"
 9       puts "Hash inner key=>#{key}"
10       puts "Hash inner value=>#{value}"
11    else
12       puts "single Hash=>#{value}"
13    end

In the example cited in Listing 2, Line 1 is the place where the URL parameters are actually parsed. In Rails, the URL parameters are stored in the default object "params". "params" has a "Hash data structure". If the "key" happens to be a simple value, it gets printed out as shown in Line 12. In this particular case, the field "key" might hold the values "user", "id", and "arr" respectively. If the parameter is of type array (in this case "arr" parameter), again the values are printed out. However, in the case of a complex type like "user", the values are further parsed to extract the subkeys and their respective values. In the case of "user", the subkey "k" will hold the values "first_name" and "last_name" respectively, in subsequent iterations.

Page 1 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date