Controller Flashcards
What is rails middleware?
Rails middleware allows you to catch request or response before it reaches Rails, and modify it. ( You are in the middle between Rack and Rails).
How to add and include your custom middleware?
create app/middlewares/sample.rb
class Sample def initialize app @app = app end
def call env puts "Middleware called"
@app.call(env) end end
and include config/environments/development.rb
config.middleware.use Sample
How to render a concrete template?
template path app/views/my_examples/sample.html.erb
def new
render ‘my_examples/sample’
end
How to use a redirect?
redirect_to ‘/animals/new’
What are the differences between render and redirect?
1) redirect is a new request, but render just draw the html
Can you tell me 6 ways to render a concrete template?
# inside Product controller render '/products/index.html.erb' render 'products/index.html.erb' render 'products/index.html' render 'products/index' render 'index' render :index
How to render partial?
# app/views/my_examples/_partial.html.erb render partial: '/my_examples/partial'
What path returns the following code “render @animal” ?
# AnimalsController app/views/animals/_animal.html.haml
How to render json/xml/text/body/ruby_code/js/html in controller? ( 7 options )
- render json: {a: 1}
- render xml: {a: 1}
- render plain: “Hello” # text
- render body: ‘AAA’
- render html: “<h1> dad </h1>“.html_safe
- render inline: “#{1 + 1}” # ruby
- render js: “alert(‘h’)”
How to add a new helper and use it inside the controller?
# app/helpers/sample_helper.rb module SampleHelper def hello p 'Hello' end end
inside contoller method
def new
helpers.hello
end
How to return nothing? Just a status?
head :ok
How to change layout? Or turn off layout?
# app/views/layouts/application.html.erb render layout: 'sample' # turn off render layout: false
How to change the response status?
render status: 401
How to redirect and send a flash message?
redirect_to ‘/animals/new’, notice: ‘hello’
#
How to add your own flash name for redirection?
class ApplicationController < ActionController::Base add_flash_types :my_notice end
redirect_to ‘/animals/new’, my_notice: ‘hello’
#
How to share variables between controller and view?
View creates a copy of all instance variables from the controller.
# controller def new @a = 1 end # view
How to run something before/after action? (callback)
before_action :say_hello
after_action :say_hello
private
def say_hello
p ‘Hello’ * 100
end
How to run something before before_action?
prepend_before_action :before_hello
prepend_after_action :before_hello
What is a session?
A session is a concept to store data during one request that you can read during later requests. And typically the session only persists until the user shuts down their browser.
What are cookies?
A cookie is a small text file stored in the browser.
What are the differences between sessions and cookies?
Session the abstract concept of holding temporary data. Cookies one (common) way of doing it.
In rails they are similar but not the same. session is an entire hash that gets put in the secure session cookie that expires when the user closes the browser ( we store only one cookies which is secure this is session)
Where we can store session?
- Inside cookies
- Inside the database (activerecord-session_store)
- Inside the cache ( redis )
How to create cookies/session?
# controller cookies[:sample] = 'Cookies Hello' session[:sample] = 'Session hello' # view
= cookies[:sample]
= session[:sample]
How to clear cookies/session?
cookies[:sample] = ‘Cookies Hello’
session[:sample] = ‘Session hello’
cookies. delete :sample session. delete :sample
What are the biggest disadvantages of using cookies?
- You can only store about 4kb of data in a cookie.
- Cookies are sent along with every request you make.
- (security) If you accidentally expose your secret_key_base, your users can change the data you’ve put inside your cookie
What should we include on the controller level?
- Security (authentication, authorization)
- Parsing and white-listing parameters
- Loading or instantiating the model ( or services )
- Deciding which view to render (
or how are we going to show result and error )