Jump To Content

LearnHub




Action Controller: Parameters

Parameters

You will probably want to access data sent in by the user or other parameters in your controller actions. There are two kinds of parameters possible in a web application. The firs t are parameters that are sent as part of the URL, query string parameters. The query string is everything after "?" in the URL. The second type of parameter is usually referred t o as POST data. This information usually comes from a HTML form which has been filled in by the user. It's called POST data because it can only be sent as part of an HTTP POST req uest. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the `params` hash in your controller:


class ClientsController < ActionController::Base

  # This action uses query string parameters because it gets run by a HTTP GET request,
  # but this does not make any difference to the way in which the parameters are accessed.
  # The URL for this action would look like this in order to list activated clients: /clients?status=activated
  def index
    if params[:status] = "activated" 
      @clients = Client.activated
    else
      @clients = Client.unativated
    end
  end

  # This action uses POST parameters. They are most likely coming from an HTML
  # form which the user has submitted. The URL for this RESTful request will
  # be "/clients", and the data will be sent as part of the request body.
  def create
    @client = Client.new(params[:client])
    if @client.save
      redirect_to @client
    else
      # This line overrides the default rendering behavior, which would have been
      # to render the "create" view.
      render :action => "new" 
    end
  end

end

Hash and array parameters

The params hash is not limited to one-dimensional keys and values. It can contain arrays and (nested) hashes. To send an array of values, append "[]" to the key name:


GET /clients?ids[]=1&ids[2]&ids[]=3

The value of `params[:ids]` will now be `["1", "2", "3"]`. Note that parameter values are always strings; Rails makes no attempt to guess or cast the type.

To send a hash you include the key name inside the brackets:


<form action="/clients" method="post">
  <input type="text" name="client[name]" value="Acme" />
  <input type="text" name="client[phone]" value="12345" />
  <input type="text" name="client[address][postcode]" value="12345" />
  <input type="text" name="client[address][city]" value="Carrot City" />
</form>

The value of `params[:client]` when this form is submitted will be `{:name => "Acme", :phone => "12345", :address => {:postcode => "12345", :city => "Carrot City"}}`.

Routing parameters

The `params` hash will always contain the `:controller` and `:action` keys, but you should use the methods `controller_name` and `action_name` instead to access these values. Any other parameters defined by the routing, such as `:id` will also be available.

Articles in this guide

  1. Introduction
  2. What does a controller do?
  3. Parameters (This article)
  4. Sessions
  5. Cookies
  6. Filters
  7. Verification
  8. The request and response objects
  9. HTTP Basic Authentication
  10. Streaming and file downloads
  11. Parameter filtering
  12. Rescue

Thanks to the Ruby on Rails documentation team

This guide was written by Tore Darrell as part of the Ruby on Rails Documentation Project and is provided freely under a Creative Commons licence


  1. SWEngineer saidThu, 12 Aug 2010 15:53:48 -0000 ( Link )

    I just wanted to note a small typo in the second line:

    The “firs t” are parameters that…

    As there is a space between “s” and “t” in “first”.

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

  2. SWEngineer saidThu, 12 Aug 2010 15:55:59 -0000 ( Link )

    Space on …referred " t o " as POST data.

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

  3. SWEngineer saidThu, 12 Aug 2010 15:56:35 -0000 ( Link )

    Space in HTTP POST “req uest”

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

  4. SWEngineer saidMon, 20 Sep 2010 14:43:53 -0000 ( Link )

    I just want to note what it seems a typo here:

    Client.unativated

    I think it should be: unactivated

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

  5. kbassiri saidWed, 27 Feb 2013 16:15:42 -0000 ( Link )

    I appreciate nuvvo. There are 2 typos in the following command.

    GET /clients?ids[]=1&ids2&ids[]=3

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

Your Comment
Textile is Enabled (View Reference)