classClientsController<ApplicationController# This action uses query string parameters because it gets run# by an HTTP GET request, but this does not make any difference# to how the parameters are accessed. The URL for# this action would look like this to list activated# clients: /clients?status=activateddefindexif params[:status] =="activated" @clients =Client.activatedelse @clients =Client.inactivatedendend# This action uses POST parameters. They are most likely coming# from an HTML form that the user has submitted. The URL for# this RESTful request will be "/clients", and the data will be# sent as part of the request body.defcreate @client =Client.new(params[:client])if @client.save redirect_to @clientelse# This line overrides the default rendering behavior, which# would have been to render the "create" view. render "new"endendend
Your application has a session for each user in. The session is only available in the controller and the view and can use one of several of different storage mechanisms:
All session stores use a cookie to store a unique ID for each session.
If you need a different session storage mechanism, you can change it in an initializer:
Rails sets up a session key (the name of the cookie) when signing the session data. These can also be changed in an initializer:
You can also pass a :domain key and specify the domain name for the cookie:
Rails sets up (for the CookieStore) a secret key used for signing the session data in config/credentials.yml.enc. This can be changed with bin/rails credentials:edit.
Accessing the Session
Session values are stored using key/value pairs like a hash:
To store something in the session, just assign it to the key like a hash:
To remove something from the session, delete the key/value pair:
class WebController < ApplicationController
rescue_from ActiveRecord::RecordNotFound, with: :error_not_found
def error_not_found(error)
return render template: 'errors/404', layout: false, status: :not_found
end
end
params.permit(:id)
def product_params
params.require(:product).permit(:name, data: {})
end
# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rails g active_record:session_migration")
# Rails.application.config.session_store :active_record_store
# Be sure to restart your server when you modify this file.
Rails.application.config.session_store :cookie_store, key: '_your_app_session'
# Be sure to restart your server when you modify this file.
Rails.application.config.session_store :cookie_store, key: '_your_app_session', domain: ".example.com"
class ApplicationController < ActionController::Base
private
# Finds the User with the ID stored in the session with the key
# :current_user_id This is a common way to handle user login in
# a Rails application; logging in sets the session value and
# logging out removes it.
def current_user
@_current_user ||= session[:current_user_id] &&
User.find_by(id: session[:current_user_id])
end
end
class LoginsController < ApplicationController
# "Create" a login, aka "log the user in"
def create
if user = User.authenticate(params[:username], params[:password])
# Save the user ID in the session so it can be used in
# subsequent requests
session[:current_user_id] = user.id
redirect_to root_url
end
end
end
class LoginsController < ApplicationController
# "Delete" a login, aka "log the user out"
def destroy
# Remove the user id from the session
session.delete(:current_user_id)
# Clear the memoized current user
@_current_user = nil
redirect_to root_url
end
end
class ApplicationController < ActionController::Base
before_action :require_login
private
def require_login
unless logged_in?
flash[:error] = "You must be logged in to access this section"
redirect_to new_login_url # halts request cycle
end
end
end
class LoginsController < ApplicationController
skip_before_action :require_login, only: [:new, :create]
end