Controller Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is rails middleware?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to add and include your custom middleware?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to render a concrete template?

A

template path app/views/my_examples/sample.html.erb
def new
render ‘my_examples/sample’
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to use a redirect?

A

redirect_to ‘/animals/new’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the differences between render and redirect?

A

1) redirect is a new request, but render just draw the html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can you tell me 6 ways to render a concrete template?

A
# 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to render partial?

A
# app/views/my_examples/_partial.html.erb
render partial: '/my_examples/partial'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What path returns the following code “render @animal” ?

A
# AnimalsController
app/views/animals/_animal.html.haml
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to render json/xml/text/body/ruby_code/js/html in controller? ( 7 options )

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to add a new helper and use it inside the controller?

A
# app/helpers/sample_helper.rb
module SampleHelper
  def hello
    p 'Hello'
  end
end

inside contoller method
def new
helpers.hello
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to return nothing? Just a status?

A

head :ok

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to change layout? Or turn off layout?

A
# app/views/layouts/application.html.erb
render layout: 'sample'
# turn off
render layout: false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to change the response status?

A

render status: 401

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to redirect and send a flash message?

A

redirect_to ‘/animals/new’, notice: ‘hello’

#

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to add your own flash name for redirection?

A
class ApplicationController < ActionController::Base
  add_flash_types :my_notice
end

redirect_to ‘/animals/new’, my_notice: ‘hello’

#

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to share variables between controller and view?

A

View creates a copy of all instance variables from the controller.

# controller
  def new
    @a = 1
  end
# view
17
Q

How to run something before/after action? (callback)

A

before_action :say_hello
after_action :say_hello

private

def say_hello
p ‘Hello’ * 100
end

18
Q

How to run something before before_action?

A

prepend_before_action :before_hello

prepend_after_action :before_hello

19
Q

What is a session?

A

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.

20
Q

What are cookies?

A

A cookie is a small text file stored in the browser.

21
Q

What are the differences between sessions and cookies?

A

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)

22
Q

Where we can store session?

A
  1. Inside cookies
  2. Inside the database (activerecord-session_store)
  3. Inside the cache ( redis )
23
Q

How to create cookies/session?

A
# controller
    cookies[:sample] = 'Cookies Hello'
    session[:sample] = 'Session hello'
# view

= cookies[:sample]
= session[:sample]

24
Q

How to clear cookies/session?

A

cookies[:sample] = ‘Cookies Hello’
session[:sample] = ‘Session hello’

cookies. delete :sample
session. delete :sample
25
Q

What are the biggest disadvantages of using cookies?

A
  1. You can only store about 4kb of data in a cookie.
  2. Cookies are sent along with every request you make.
  3. (security) If you accidentally expose your secret_key_base, your users can change the data you’ve put inside your cookie
26
Q

What should we include on the controller level?

A
  • 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 )