Guide Targets:
- How to follow the flow of a request through a controller?
- routes -> controller#action -> model -> c -> view -> c -> client
- How to restrict parameters passed to your controller?
- use strong parameters! with
requireandpermit
- use strong parameters! with
- How and why to store data in the session or cookies?
- use
sessionandcookiesinstances, make conversation :P
- use
- How to work with filters to execute code during request processing?
- use
before_actionto halt flow when something bad happen..
- use
- How to use Action Controller's built-in HTTP authentication.
- basic auth and digest auth, not try.
- How to stream data directly to the users's browser?
- use
send_dataandsend_filebetter restful way.
- use
- How to filter sensitive parameters so they do not appear in app's log?
- some config like
config.filter_parameters
- some config like
- How to deal with exceptions that may be raised during request processing?
- use
rescue_fromto handle specified exceptions.
- use
My Notes
ActionController
- get
controllerandactionname from - params
params[:controller] params[:action] - methods
controller_name, action_name
Default URL params
default_url_options
Request
request.class => ActionDispatch::Request- methods1:
host, domain(n), port, protocol, url, query_string - methods2:
method, get?, post?, put?, patch?, delete?, head? - methods3:
format, headers, body, remote_ip - parameters1:
path_parametersfrom routing - parameters2:
query_parametersfrom query string - parameters3:
request_parametersfrom post body
Response
response.class => ActionDispatch::Response- methods:
headers, body, location, content_type, charset
Strong Parameters
-
requireto specify required params -
permitto specify permit params (danger to usepermit!) params.require(:foo).permit(:a, :b, :c)params.fetch(:bar, {}).permit(:a, :b, :c)
Session
ActionDispatch::Session::CookieStoreActionDispatch::Session::CacheStore- use a cookie to store uniq id for each session
- change secret will invalid all CookieStore session.
session.class => ActionDispatch::Request::Sessionsession[:user_id] = user.id # loginsession[:user_id] = nil # logoutreset_session
Flash
flash.class => ActionDispatch::Flash::FlashHashredirect path, notice: "msg"redirect path, alert: "msg"redirect path, flash: { foo: 'bar' }-
flash.keepkeep to next request -
flash.nowrender right now
Cookie
cookies.class => ActionDispatch::Cookies::CookieJarcookies.delete(:key)
Hooks
before_actionskip_before_actionafter_actionround_action
Request Forgery Protection
from_authenticity_token
Streaming and File Downloading
send_datasend_file- resourceful render