aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/source/actioncontroller_basics/params.txt
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-11-06 01:10:30 +0530
committerPratik Naik <pratiknaik@gmail.com>2008-11-06 01:10:30 +0530
commit396d599e24cbfa15c62b20fab8cc02cdba046ce3 (patch)
tree11dd8368e2674342098af21136893bee491c44ff /railties/doc/guides/source/actioncontroller_basics/params.txt
parent32089cbcc9ca3fb935f783e7a7ef2b60b7d43006 (diff)
downloadrails-396d599e24cbfa15c62b20fab8cc02cdba046ce3.tar.gz
rails-396d599e24cbfa15c62b20fab8cc02cdba046ce3.tar.bz2
rails-396d599e24cbfa15c62b20fab8cc02cdba046ce3.zip
Update guides from docrails
Diffstat (limited to 'railties/doc/guides/source/actioncontroller_basics/params.txt')
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/params.txt43
1 files changed, 35 insertions, 8 deletions
diff --git a/railties/doc/guides/source/actioncontroller_basics/params.txt b/railties/doc/guides/source/actioncontroller_basics/params.txt
index 7f494d7c9b..fb380519fd 100644
--- a/railties/doc/guides/source/actioncontroller_basics/params.txt
+++ b/railties/doc/guides/source/actioncontroller_basics/params.txt
@@ -1,14 +1,15 @@
== 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:
+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, called 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
+ # 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
@@ -34,12 +35,12 @@ class ClientsController < ActionController::Base
end
-------------------------------------
-=== Hash and array parameters ===
+=== 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
+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.
@@ -57,6 +58,32 @@ To send a hash you include the key name inside the brackets:
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 ===
+=== 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.
+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. As an example, consider a listing of clients where the list can show either active or inactive clients. We can add a route which captures the `:status` parameter in a "pretty" URL:
+
+[source, ruby]
+------------------------------------
+# ...
+map.connect "/clients/:status", :controller => "clients", :action => "index", :foo => "bar"
+# ...
+------------------------------------
+
+In this case, when a user opens the URL `/clients/active`, `params[:status]` will be set to "active". When this route is used, `params[:foo]` will also be set to "bar" just like it was passed in the query string in the same way `params[:action]` will contain "index".
+
+=== `default_url_options` ===
+
+You can set global default parameters that will be used when generating URLs with `default_url_options`. To do this, define a method with that name in your controller:
+
+------------------------------------
+class ApplicationController < ActionController::Base
+
+ #The options parameter is the hash passed in to url_for
+ def default_url_options(options)
+ {:locale => I18n.locale}
+ end
+
+end
+------------------------------------
+
+These options will be used as a starting-point when generating, so it's possible they'll be overridden by url_for. Because this method is defined in the controller, you can define it on ApplicationController so it would be used for all URL generation, or you could define it on only one controller for all URLs generated there.