Introduction
"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.
http://localhost/users/create?user[first_name]=dhanabal&user
[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.
"user[first_name]=dhanabal&user[last_name]=arumugam&arr
[]=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).
Arrays
Read the Rest of this Article at Developer.com