rails


Let's study about Controller!

(This slides is based on guides.rubyonrails.org)

What is the controller?



(Action)Controller is the C in MVC(1)







(1)  http://ko.wikipedia.org/wiki/MVC_패턴

What Does the Controller do?


Making a output
and
Understanding a request

http://ruby.railstutorial.org/chapters/a-demo-app#fig-mvc_detailed

Naming convention


Rails like pluralization.

not like           ->               like
ex) UserController -> UsersController
PlaceController -> PlacesController

Parameters

Q: How to get each data that is POST and GET type?

A: Rails does not make any distinction between query string parameters and POST parameters, and both are available in the params hash in your controller.

Parameters

Request
In Controller

parameters

Request
GET /persons?me[]=student&me[]=worker&me[]=newbie

In controller
params[:me] = ["student", "worker", "newbie"]

PARAMETERS

Request
POST /persons HTTP 1.1\r\n
HOST: ex.kr\r\n
Content-Type: application/json\r\n
\r\n
{"information": {"name": "acme", "addr": "123" }}

In controller
params[:information] = {name: "acme", addr: "123"}

PARAMETERS

Routing Parameters
get '/clients/:type' => 'clients#index', who: 'me!' # in route.rb

Request
GET /clients/hogang

In controller
params[:who] = "me!"
params[:type] = "hogang"

PARAMETERS

Routing Parameters
get '/clients/:type' => 'clients#index', who: 'me!' # in route.rb

Request
GET /clients/hogang?who=you!

In controller
params[:who] = "???"

PARAMETERS

Routing Parameters
get '/clients/:type' => 'clients#index', who: 'me!' # in route.rb

Request
GET /clients/hogang?who=you!

In controller
params[:who] = "me!"

Session

setting: config/initializers/session_store.rb

  • Session::CookieStore
  • Session::CacheStore
  • Session::ActiveRecordStore(require activerecord-session_store gem)
  • Session::MemCacheStore - (this is a legacy implementation; consider using CacheStore instead)

session

Storing the Session
session[:current_user_id] = 'root'

Accessing the Session
@user_id = session[:current_user_id]

Deleting the Session
session[:current_user_id] = nil

Cookies

Stroring the Cookie
cookies[:like] = 'chikchok'

Accessing the Cookie
@kooki = cookies[:like]

Deleting the Cookie
cookies.delete(:like)

Rendering



Response depends on on 'Accept' field in request

Streaming and File Downloads


'send_data' is similar to 'send_file'

Thank you

Made with Slides.com