Could anyone explain
params in Rails controller: where they come from, and what they are referencing?
def create @vote = Vote.new(params[:vote]) item = params[:vote][:item_id] uid = params[:vote][:user_id] @extant = Vote.find(:last, :conditions => ["item_id = ? AND user_id = ?", item, uid]) last_vote_time = @extant.created_at unless @extant.blank? curr_time = Time.now end
I would like to be able to read this code line-by-line and understand what’s going on.
The params come from the user’s browser when they request the page. For an HTTP GET request, which is the most common, the params are encoded in the url. For example, if a user’s browser requested
params[:foo] would be “1” and
params[:boo] would be “octopus”.
In HTTP/HTML, the params are really just a series of key-value pairs where the key and the value are strings, but Ruby on Rails has a special syntax for making the params be a hash with hashes inside. For example, if the user’s browser requested
params[:vote] would be a hash,
params[:vote][:item_id] would be “1” and
params[:vote][:user_id] would be “2”.
The Ruby on Rails params are the equivalent of the $_REQUEST array in PHP.
As others have pointed out,
params values can come from the query string of a GET request, or the form data of a POST request, but there’s also a third place they can come from: The path of the URL.
As you might know, Rails uses something called routes to direct requests to their corresponding controller actions. These routes may contain segments that are extracted from the URL and put into
params. For example, if you have a route like this:
match 'products/:id', ...
Then a request to a URL like
http://example.com/products/42 will set
Params contains the following three groups of parameters:
- User supplied parameters
- GET (http://domain.com/url?param1=value1¶m2=value2 will set params[:param1] and params[:param2])
- POST (e.g. JSON, XML will automatically be parsed and stored in params)
- Note: By default, Rails duplicates the user supplied parameters and stores them in params[:user] if in UsersController, can be changed with wrap_parameters setting
- Routing parameters
match '/user/:id'in routes.rb will set params[:id]
- Default parameters
params[:action]is always available and contains the current controller and action
Basically, parameters are user specified data to rails application.
When you post a form, you do it generally with POST request as opposed to GET request. You can think normal rails requests as GET requests, when you browse the site, if it helps.
When you submit a form, the control is thrown back to the application. How do you get the values you have submitted to the form?
params is how.
About your code.
@vote = Vote.new params[:vote] creates new Vote to database using data of params[:vote]. Given your form user submitted was named under name :vote, all data of it is in this :vote field of the hash.
Next two lines are used to get item and uid user has submitted to the form.
@extant = Vote.find(:last, :conditions => ["item_id = ? AND user_id = ?", item, uid])
finds newest, or last inserted, vote from database with conditions item_id = item and user_id = uid.
Next lines takes last vote time and current time.
On the Rails side,
params is a method that returns an