aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/actioncontroller/params.txt
diff options
context:
space:
mode:
Diffstat (limited to 'railties/doc/guides/actioncontroller/params.txt')
-rw-r--r--railties/doc/guides/actioncontroller/params.txt62
1 files changed, 62 insertions, 0 deletions
diff --git a/railties/doc/guides/actioncontroller/params.txt b/railties/doc/guides/actioncontroller/params.txt
new file mode 100644
index 0000000000..67f97b6135
--- /dev/null
+++ b/railties/doc/guides/actioncontroller/params.txt
@@ -0,0 +1,62 @@
+== 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 first 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 to 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 request. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the `params` hash in your controller:
+
+[source, ruby]
+-------------------------------------
+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">
+ <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"}}`. Note the nested hash in `params[:client][:address]`.
+
+=== 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.