9 redirect_to root_url, notice: "Good."
13 session[:user_id] = nil
15 flash[:notice] = "Hello" # Gets flushed on next request
16 flash.keep # Persist flash values
17 flash.now[:error] = "Boo" # Available on the same request
19 cookies[:hello] = "Hi"
23 # params is a combination of:
30 respond_to do |format|
32 format.xml { render xml: @users }
33 format.json { render json: @users }
34 format.js # Will be executed by the browser
37 ### default_url_options
39 # The options parameter is the hash passed in to 'url_for'
40 def default_url_options(options)
41 {:locale => I18n.locale}
46 # Filter with callbacks
47 before_filter :authenticate
48 before_filter :authenticate, except: [:login]
49 before_filter :authenticate, only: [:login]
51 redirect_to login_url unless controller.logged_in?
55 before_filter do |controller|
56 redirect_to login_url unless controller.logged_in?
59 # Filter with external classes
60 before_filter LoginFilter
62 def self.filter(controller) ...; end
66 skip_before_filter :require_login, only: [:new, :create]
68 # Before/after filters
69 around_filter :wrap_in_transaction
70 def wrap_in_transaction(&blk)
71 ActiveRecord::Base.transaction { yield }
74 ### HTTP basic authentication
76 before_filter :authenticate
78 # Basic authentication:
80 authenticate_or_request_with_http_basic { |u, p|
81 u == "root" && p == "alpine"
85 # ...or digest (hashed) authentication:
86 # uses the ha1 hash (username:realm:password)
87 def authenticate_by_digest
90 "rsc" => Digest::MD5.hexdigest("rsc:#{realm}:passwordhere")
93 authenticate_or_request_with_http_digest(realm) { |user|
98 # For integration tests
100 auth = ActionController::HttpAuthentication::Basic.encode_credentials(user, pass)
101 get "/notes/1.xml", nil, 'HTTP_AUTHORIZATION' => auth
105 is_logged_in = authenticate_with_http_token do |token, options|
106 token == our_secret_token
109 request_http_token_authentication unless is_logged_in
113 request.host #=> "www.example.com"
114 request.domain #=> "www.example.com"
115 request.domain(n=2) #=> "example.com"
117 request.protocol #=> "http://"
118 request.query_string #=> "q=duck+tales"
119 request.url #=> "http://www.example.com/search?q=duck+tales"
120 request.fullpath #=> "/search?q=duck+tales"
122 request.headers # Returns a hash
124 request.format #=> "text/html"
125 request.remote_ip #=> "203.167.220.220"
126 request.local? #=> true (if localhost/127.0.0.1)
130 request.method #=> "POST"
131 request.method_symbol #=> :post
141 response.status #=> 404
142 response.location # Redirect location
143 response.content_type
147 response.headers["Content-Type"] = "application/pdf"
151 send_data pdfdata, filename: "foo.pdf", type: "application/pdf"
152 send_file Rails.root.join('public','filename.txt') [filename: '..', type: '..']
156 * [Guide](http://guides.rubyonrails.org/action_controller_overview.html)
157 * [HttpAuthentication::Basic](http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic.html)
158 * [HttpAuthentication::Token](http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Token.html)