diff options
author | Xavier Noria <fxn@hashref.com> | 2011-05-14 11:21:27 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2011-05-14 11:21:27 +0200 |
commit | d49113023676fbf0b6653730f5bde759990caede (patch) | |
tree | de8a47655ad3a1fa1e73dbc307ec9b806637b3ec /railties/guides/source/action_controller_overview.textile | |
parent | db886c817c3620ceaa68a988947e49e5219532fd (diff) | |
parent | e5524d538c0d0f39d655a78fc45d2122c0ff2f2a (diff) | |
download | rails-d49113023676fbf0b6653730f5bde759990caede.tar.gz rails-d49113023676fbf0b6653730f5bde759990caede.tar.bz2 rails-d49113023676fbf0b6653730f5bde759990caede.zip |
Merge branch 'master' of git://github.com/lifo/docrails
Conflicts:
actionpack/lib/action_view/helpers/date_helper.rb
railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt
Diffstat (limited to 'railties/guides/source/action_controller_overview.textile')
-rw-r--r-- | railties/guides/source/action_controller_overview.textile | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/railties/guides/source/action_controller_overview.textile b/railties/guides/source/action_controller_overview.textile index 3a1a4ee66e..891bae3d5e 100644 --- a/railties/guides/source/action_controller_overview.textile +++ b/railties/guides/source/action_controller_overview.textile @@ -110,6 +110,32 @@ When this form is submitted, the value of +params[:client]+ will be <tt>{"name" Note that the +params+ hash is actually an instance of +HashWithIndifferentAccess+ from Active Support, which acts like a hash that lets you use symbols and strings interchangeably as keys. +h4. JSON/XML parameters + +If you're writing a web service application, you might find yourself more comfortable on accepting parameters in JSON or XML format. Rails will automatically convert your parameters into +params+ hash, which you'll be able to access like you would normally do with form data. + +So for example, if you are sending this JSON parameter: + +<pre> +{ "company": { "name": "acme", "address": "123 Carrot Street" } } +</pre> + +You'll get <tt>params[:company]</tt> as <tt>{ :name => "acme", "address" => "123 Carrot Street" }</tt>. + +Also, if you've turned on +config.wrap_parameters+ in your initializer or calling +wrap_parameters+ in your controller, you can safely omit the root element in the JSON/XML parameter. The parameters will be cloned and wrapped in the key according to your controller's name by default. So the above parameter can be written as: + +<pre> +{ "name": "acme", "address": "123 Carrot Street" } +</pre> + +And assume that you're sending the data to +CompaniesController+, it would then be wrapped in +:company+ key like this: + +<ruby> +{ :name => "acme", :address => "123 Carrot Street", :company => { :name => "acme", :address => "123 Carrot Street" }} +</ruby> + +You can customize the name of the key or specific parameters you want to wrap by consulting the "API documentation":http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html + h4. 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. 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: |