Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / source / actioncontroller_basics / verification.txt
1 == Verification ==
2
3 Verifications make sure certain criteria are met in order for a controller or action to run. They can specify that a certain key (or several keys in the form of an array) is present in the `params`, `session` or `flash` hashes or that a certain HTTP method was used or that the request was made using XMLHTTPRequest (Ajax). The default action taken when these criteria are not met is to render a 400 Bad Request response, but you can customize this by specifying a redirect URL or rendering something else and you can also add flash messages and HTTP headers to the response. It is described in the link:http://api.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html[API documentation] as "essentially a special kind of before_filter".
4
5 Here's an example of using verification to make sure the user supplies a username and a password in order to log in:
6
7 [source, ruby]
8 ---------------------------------------
9 class LoginsController < ApplicationController
10
11 verify :params => [:username, :password],
12 :render => {:action => "new"},
13 :add_flash => {:error => "Username and password required to log in"}
14
15 def create
16 @user = User.authenticate(params[:username], params[:password])
17 if @user
18 flash[:notice] = "You're logged in"
19 redirect_to root_url
20 else
21 render :action => "new"
22 end
23 end
24
25 end
26 ---------------------------------------
27
28 Now the `create` action won't run unless the "username" and "password" parameters are present, and if they're not, an error message will be added to the flash and the "new" action will be rendered. But there's something rather important missing from the verification above: It will be used for *every* action in LoginsController, which is not what we want. You can limit which actions it will be used for with the `:only` and `:except` options just like a filter:
29
30 [source, ruby]
31 ---------------------------------------
32 class LoginsController < ApplicationController
33
34 verify :params => [:username, :password],
35 :render => {:action => "new"},
36 :add_flash => {:error => "Username and password required to log in"},
37 :only => :create # Only run this verification for the "create" action
38
39 end
40 ---------------------------------------