@@ -106,7 +106,13 @@ To send a hash you include the key name inside the brackets:
</form>
</html>
When this form is submitted, the value of +params[:client]+ will be <tt>{"name" => "Acme", "phone" => "12345", "address" => {"postcode" => "12345", "city" => "Carrot City"}}</tt>. Note the nested hash in +params[:client][:address]+.
When this form is submitted, the value of +params[:client]+ will be:
Note the nested hash in +params[:client][:address]+.
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.
...
...
@@ -120,7 +126,7 @@ So for example, if you are sending this JSON parameter:
You'll get <tt>params[:company]</tt> as <tt>{ :name => "acme", "address" => "123 Carrot Street" }</tt>.
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:
...
...
@@ -131,7 +137,7 @@ Also, if you've turned on +config.wrap_parameters+ in your initializer or callin
And assume that you're sending the data to +CompaniesController+, it would then be wrapped in +:company+ key like this:
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
...
...
@@ -141,7 +147,7 @@ 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:
<ruby>
match '/clients/:status' => 'clients#index', :foo => "bar"
match '/clients/:status' => 'clients#index', foo: 'bar'
</ruby>
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".
...
...
@@ -153,7 +159,7 @@ You can set global default parameters for URL generation by defining a method ca
<ruby>
class ApplicationController <ActionController::Base
defdefault_url_options
{:locale => I18n.locale}
{locale:I18n.locale}
end
end
</ruby>
...
...
@@ -196,7 +202,7 @@ Rails sets up a session key (the name of the cookie) when signing the session da
<ruby>
# Be sure to restart your server when you modify this file.
Rails sets up (for the CookieStore) a secret key used for signing the session data. This can be changed in +config/initializers/secret_token.rb+
...
...
@@ -297,9 +303,9 @@ end
Note that it is also possible to assign a flash message as part of the redirection. You can assign +:notice+, +:alert+ or the general purpose +:flash+:
<ruby>
redirect_to root_url, :notice => "You have successfully logged out."
The +destroy+ action redirects to the application's +root_url+, where the message will be displayed. Note that it's entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It's conventional to display any error alerts or notices from the flash in the application's layout:
...
...
@@ -359,7 +365,7 @@ class ClientsController < ApplicationController
#...
else
flash.now[:error] = "Could not save client"
render:action =>"new"
renderaction:"new"
end
end
end
...
...
@@ -373,7 +379,7 @@ Your application can store small amounts of data on the client -- called cookies
@@ -389,7 +395,7 @@ class CommentsController < ApplicationController
end
redirect_to@comment.article
else
render :action => "new"
renderaction:"new"
end
end
end
...
...
@@ -407,14 +413,14 @@ class UsersController < ApplicationController
@users = User.all
respond_todo|format|
format.html#index.html.erb
format.xml{render:xml =>@users}
format.json{render:json =>@users}
format.xml{renderxml:@users}
format.json{renderjson:@users}
end
end
end
</ruby>
Notice that in the above case code is <tt>render :xml => @users</tt> and not <tt>render :xml => @users.to_xml</tt>. That is because if the input is not string then rails automatically invokes +to_xml+ .
Notice that in the above case code is <tt>render xml: @users</tt> and not <tt>render xml: @users.to_xml</tt>. That is because if the input is not string then rails automatically invokes +to_xml+ .
h3. Filters
...
...
@@ -455,7 +461,7 @@ In this example the filter is added to +ApplicationController+ and thus all cont