You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# supports only notice and alert by default# the rest has to go into flash hashredirect_to:index,notice: "success"redirect_to:new,notice: "errors"redirect_to:new,flash: {success: "yeah"}flash[:info]="updated"flash.now[:notice]="wrong params"# sets the flash data in the current requestflash.keep# keeps the flash data for the next request
Callbacks
before_action:do_authentication# calls the methodafter_action:do_compresssionaround_action:do_something# your method needs to call yieldskip_before_actionskip_after_actionskip_around_actionbefore_action:do_authentication,only: [:create,:delete]before_action:do_authentication,except: [:create,:delete]
respond_to:json# and works with respond_with Products.allrespond_todo |format|
format.html{render:index}format.json{renderxml: @products.to_json}format.js{render'partials'}# for AJAX calls with button_to remote: trueformat.xml{renderxml: @products.to_xml
Filtering for authentication
classApplicationController < ActionController::Basebefore_action:abacus_authenticate,except: :logindeflogin# do loginenddefabacus_authenticaterendertext: 'Unauthorized Access',status: '401'ifsession[:user_id].nil?endend
action_name
The name of the action currently being processed.
cookies
The cookies associated with the request. Setting values into this object stores cookies on the browser when the response is sent. Rails support for sessions is based on cookies. We discuss sessions in Rails Sessions, on page 339.
headers
A hash of HTTP headers that will be used in the response. By default, Cache-Control is set to no-cache. You might want to set Content-Type headers for special-purpose applications. Note that you shouldn’t set cookie values in the header directly—use the cookie API to do this.
params
A hash-like object containing request parameters (along with pseudopa- rameters generated during routing). It’s hash-like because you can index entries using either a symbol or a string—params[:id] and params['id'] return the same value. Idiomatic Rails applications use the symbol form.
request
The incoming request object. It includes these attributes:
request_method returns the request method, one of :delete, :get, :head, :post, or :put.
method returns the same value as request_method except for :head, which it returns as :get because these two are functionally equivalent from an application point of view.
delete?, get?, head?, post?, and put? return true or false based on the request method.
xml_http_request? and xhr? return true if this request was issued by one of the Ajax helpers. Note that this parameter is independent of the method parameter.
url(), which returns the full URL used for the request.
protocol(), host(), port(), path(), and query_string(), which return components of the URL used for the request, based on the following pattern: proto- col://host:port/path?query_string.
domain(), which returns the last two components of the domain name of the request.
host_with_port(), which is a host:port string for the request.
port_string(), which is a :port string for the request if the port is not the default port (80 for HTTP, 443 for HTTPS).
ssl?(), which is true if this is an SSL request; in other words, the request was made with the HTTPS protocol.
remote_ip(), which returns the remote IP address as a string. The string may have more than one address in it if the client is behind a proxy.
env(), the environment of the request. You can use this to access values set by the browser, such as this: request.env['HTTP_ACCEPT_LANGUAGE']
accepts(), which is an array with Mime::Type objects that represent the MIME types in the Accept header.
format(), which is computed based on the value of the Accept header, with Mime::HTML as a fallback.
content_type(), which is the MIME type for the request. This is useful for put and post requests.
headers(), which is the complete set of HTTP headers.
body(), which is the request body as an I/O stream.
content_length(), which is the number of bytes purported to be in the body.
Rails leverages a gem named Rack to provide much of this functionality.
See the documentation of Rack::Request for full details.
response
The response object, filled in during the handling of the request. Normally, this object is managed for you by Rails. As we’ll see when we look at callbacks in Callbacks, on page 346, we sometimes access the internals for specialized processing.
session
A hash-like object representing the current session data. We describe this in Rails Sessions, on page 339.
In addition, a logger is available throughout Action Pack.