Please enable JavaScript.
Coggle requires JavaScript to display documents.
:passport_control: API / Controllers (:watch: controller (parameters…
:passport_control:
API / Controllers
:diamond_shape_with_a_dot_inside:
api
basic configuration
new app
= rails new my_api --api
Configure the generators to skip generating views
ApplicationController inherit from ActionController::API
Configure your application to start
changing existing app
= process to migrate to api
choosing midleware
list of middlewares
base middlewares
rack sendfile middleware
Request json middleware
cache middleware
other middlewares
commands
ADD MIDD
=
config.middleware.use Rack::MethodOverride
REMOVE MIDD
=
config.middleware.delete ::Rack::Sendfile
Choosing controller modules
choosing controller modules
adding other modules
:watch: controller
naming convention
prefer plural to singular
ClientsController
parameters
data from requests (put, post, get, delete) are stored in params property
Hash and Array Parameters
JSON parameters
Routing parameters
- controller, action, atc..
Default url options
- global default parameter for url (etc: I18n)
Strong parameters
= will result in a
400 Bad Request
being returned if not all required parameters are passed in
Session
rails
credentials:edit
=> config/credentials.yml.enc
Accessing the session
= session[:current_user_id]
flash
= part of session. It will destroy every request and stay in session only in the next session. Use for user messages
Cookies
cookies[:key]
cookies.delete(:key)
Rendering xml and json
Filters
Filters are methods that are run "before", "after" or "around" a controller action.
examples
before_action :require_login
skip_before_action :require_login, only: [:new, :create]
extract to another class
other
Request forgery protection
Log filtering
Force https protocol
Request and Response object
Request
All in request_parameters
Response
Custom headers
HTTP Authentications
Basic
= http_basic_authenticate_with name: "humbaba", password: "5baa61e4"
Digest
= authenticate_or_request_with_http_digest
Streaming and file download
Sending files
Restfull downloads
= config/initializers/mime_types.rb
Streaming
Rescure = error handling
500, 400 templates
Rescure from
rails routes