class ActionController::Base


end
render action: “overthere” # won’t be called if monkeys is nil
end
return
redirect_to(action: “elsewhere”)
if monkeys.nil?
def do_something
“return” to halt execution.
If you need to redirect on the condition of something, then be sure to add
end
render action: “overthere” # raises DoubleRenderError
redirect_to action: “elsewhere”
def do_something
do either again will result in a DoubleRenderError:
An action may perform only a single render or a single redirect. Attempting to
## Calling multiple redirects or renders
ActionController::Redirecting.
Learn more about ‘redirect_to` and what options you have in
calls both “create” and then “show” within one request.
request (a GET to the show action), and not some internal re-routing which
external HTTP-level redirection which will cause the browser to make a second
redirected to the `show` method, which is then executed. Note that this is an
In this case, after saving our new entry to the database, the user is
end
end
# things didn’t go so well, do something else
else
redirect_to action: ‘show’, id: @entry.id
# The entry was saved correctly, redirect to show
if @entry.save
@entry = Entry.new(params)
def create
this:
action that we’ll assume has already been created. The code might look like
(Don’t Repeat Yourself), we’re going to reuse (and redirect to) a ‘show`
show the user the new entry. Because we’re following good DRY principles
`create` action, which stores a blog entry to the database, we might like to
Redirects are used to move from one action to another. For example, after a
## Redirects
Read more about writing ERB and Builder templates in ActionView::Base.
end
end
when 2..10 then render action: “show_many”
when 1 then render action: “show”
when 0 then render action: “no_results”
case @results.count
@results = Search.find(params)
def search
rendering methods:
could result in the rendering of different templates will use the manual
You don’t have to rely on the automated rendering. For example, actions that
Title: <%= @post.title %>
Which are then automatically available to the view:
end
@post = Post.find(params)
def show
view by assigning instance variables:
templates. It’s automatically configured. The controller passes objects to the
Included in the Action Pack is the Action View, which enables rendering of ERB
methods. The most versatile and common is the rendering of a template.
Action Controller sends content to the user by using one of five rendering
## Renders
intervention.
automatically through the use of renders and redirects and requires no user
sent to the user’s browser. The actual response object is generated
Each action results in a response, which holds the headers and document to be
## Responses
cookie-based sessions.
after it has expired, so you should avoid storing sensitive information in
or edit the session data. However, the user can keep a copy of the cookie even
ActionDispatch::Session::CookieStore). Thus the user will not be able to read
By default, sessions are stored in an encrypted browser cookie (see
or you can remove the entire session with ‘reset_session`.<br><br>session = nil
# removes :person from session
`nil`:
For removing objects from the session, you can either assign a single key to
“Hello #{session}”
You can retrieve it again through the same hash:<br><br>session = Person.authenticate(user_name, password)
accesses a hash:
You can place objects in the session by using the `session` method, which
keep it all synchronized – something databases already excel at.
it’s likely they could be changed unknowingly. It’s usually too much work to
login. The session should not be used, however, as a cache for objects where
are needed all the time, such as a User object for a system that requires
constructed in a multi-paged process, or objects that don’t change much and
objects that are not yet ready to be persisted, such as a Signup object
Sessions allow you to store objects in between requests. This is useful for
## Sessions
depth of the nesting.
=> { “address” => { “street” => “hyacintvej” } } }‘. There’s no limit to the
been named ‘post[street]`, the `params` would have included `{ “post”
“name” => “david”, “address” => “hyacintvej” } }`. If the address input had
A request coming from a form holding these inputs will include `{ “post” => {
<input type=“text” name=“post” value=“hyacintvej”>
<input type=“text” name=“post” value=“david”>
specifying keys using brackets, such as:
It’s also possible to construct multi-dimensional parameter hashes by
“5” }‘ in `params`.
`/posts?category=All&limit=5` will include `{ “category” => “All”, “limit” =>
method which returns a hash. For example, an action that was performed through
form data submitted through a POST request are available through the `params`
All request parameters, whether they come from a query string in the URL or
## Parameters
end
render plain: “This server hosted at #{location}”
location = request.env[“REMOTE_ADDR”]
def server_ip
used to query for HTTP headers:
The full request object is available via the request accessor and is primarily
accessor methods. Then the action is performed.
request with all the HTTP headers are made available to the action through
remaining request parameters, the session (if one is available), and the full
`action` keys. These determine which controller and action are called. The
For every request, the router determines the value of the `controller` and
## Requests
these themes.
Controllers: Get-and-show and do-and-redirect. Most actions are variations on
These two methods represent the two basic action archetypes used in Action
the user to the index action.
redirect works by returning an external `302 Moved` HTTP response that takes
its main purpose (creating a new post), it initiates a redirect instead. This
Unlike index, the create action will not render a template. After performing
populating the `@posts` instance variable.
render the template `app/views/posts/index.html.erb` by default after
the action. For example, the `index` action of the PostsController would
corresponding to the name of the controller and action after executing code in
Actions, by default, render a template in the `app/views` directory
end
end
redirect_to posts_path
@post = Post.create params[:post]
def create
end
@posts = Post.all
def index
class PostsController < ApplicationController
A sample controller could look like this:
request forgery protection and filtering of sensitive request parameters.
ApplicationController. This gives you one class to configure things such as
from `ActionController::Base`. All other controllers inherit from
By default, only the ApplicationController in a Rails application inherits
web-server through Rails Routes.
method on the controller, which will automatically be made accessible to the
template or redirects to another action. An action is defined as a public
one or more actions that are executed on request and then either it renders a
Action Controllers are the core of a web request in Rails. They are made up of
# Action Controller Base

def self.without_modules(*modules)

manually.
create a bare controller class, instead of listing the modules required
This gives better control over what you want to exclude and makes it easier to

end
end
include left
ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
class MyBaseController < ActionController::Metal

ActionController::Base except the ones passed as arguments:
Shortcut helper that returns all the modules included in
def self.without_modules(*modules)
  modules = modules.map do |m|
    m.is_a?(Symbol) ? ActionController.const_get(m) : m
  end
  MODULES - modules
end

def _protected_ivars

def _protected_ivars
  PROTECTED_IVARS
end