Controller

Strong Parameter

:๋Œ€๋Ÿ‰ ํ• ๋‹น ์ทจ์•ฝ์„ฑ์— ๋Œ€ํ•œ ํ™”์ดํŠธ ๋ฆฌ์ŠคํŠธ ๋Œ€์ฑ… ๋ฐฉ๋ฒ•์ด๋‹ค.

๋Œ€๋Ÿ‰ ํ• ๋‹น ์ทจ์•ฝ์„ฑ : ์•…์˜์ ์ธ ์‚ฌ์šฉ์ž๊ฐ€ ์ž์‹ ์˜ ๊ณ„์ •์— ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด ์‹œ์Šคํ…œ์„ ์กฐ์ž‘ํ•  ์œ„ํ—˜์ด ์žˆ์Œ.

params.require(model).permit(attr, ...)
# model:๋ชจ๋ธ์ด๋ฆ„
# attr : ์ถ”์ถœ์„ ํ—ˆ๊ฐ€ํ•  ํ•„๋“œ ์ด๋ฆ„.
  • require ๋ฉ”์„œ๋“œ๋Š” ์ง€์ •๋œ ๋ชจ๋ธ์˜ ํ‚ค๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ํ›„ ์กด์žฌํ•˜๋ฉด ํ•ด๋‹น ๊ฐ’๋“ค์„ ๋ฆฌํ„ดํ•˜๊ณ , ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ActionController::ParameterMissing ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

  • permit๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋ธ์— ์ผ๊ด„์ ์œผ๋กœ ์ถ”๊ฐ€ํ•  ์†์„ฑ๋“ค์„ ํ—ˆ๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค. ๋ฆฌํ„ด๊ฐ’์œผ๋กœ ์ง€์ •๋œ ํ‚ค๋งŒ ํฌํ•จํ•˜๋Š” ํ•ด์‹œ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

#scaffolding
def create
    @book=Book.new(book_params)
end

private

def book_params
    params.require(:book).permit(:isbn,:title,:price)
end

์ด๋ ‡๊ฒŒ Strong Parameters๋Š” ํ•œ๊บผ๋ฒˆ์— ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ๋“ค์„ ํ•„ํ„ฐ๋งํ•˜๋ฏ€๋กœ ์•…์˜์ ์ธ ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ’์„ ์ž„์˜๋กœ ๋„ฃ๋Š” ๊ฒƒ์„ ๋ฏธ์—ฐ์— ๋ฐฉ์ง€ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Filter

: ์•ก์…˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ด์ „๊ณผ ์ดํ›„์— ๋ถ€๊ฐ€ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค. ์ปจํŠธ๋กค๋Ÿฌ ์ „์ฒด์— ์ ์šฉ๋œ๋‹ค.

before & after filter(์ „,ํ›„ ์ฒ˜๋ฆฌ)

before_action :method[, ...]
after_action :method[, ...]

around filter(์ „ํ›„์ฒ˜๋ฆฌ ํ•œ๋ฒˆ์—)

around_action :method[, ...]
# example
    around_action :around_logger

    def index
        sleep3
        render text: 'index ์•ก์…˜์‹คํ–‰'
    end

    private

    def around_logger
        logger.debug('[Start1]'+Time.now.to_s)
        yield #์•ก์…˜์„ ์‹คํ–‰
        logger.debug('[Finish1]'+Time.now.to_s)
    end
end

filter ์ ์šฉ ๋ฒ”์œ„ ์ง€์ •

: ํ•„ํ„ฐ๋Š” ํ•ด๋‹น ์ปจํŠธ๋กค๋Ÿฌ + ํŒŒ์ƒ๋œ ์ปจํŠธ๋กค๋Ÿฌ๋  ์ ์šฉ๋œ๋‹ค.

only์™€ except ์˜ต์…˜( ํ•„ํ„ฐ ์ ์šฉ๋ฒ”์œ„ ์ œํ•œ)

# example
before_action :start_logger, only: [:index, index2]
after_action :end_logger, except :index

only๋Š” ์ง€์ •ํ•œ ์•ก์…˜์—๋งŒ ํ•„ํ„ฐ๋ฅผ ์ ์šฉ, except์˜ต์…˜์€ ์ง€์ •๋œ ์•ก์…˜์— ํ•„ํ„ฐ ์ ์šฉ์„ ์ œ์™ธ.

์ด ์˜ต์…˜๋“ค์€ ์ตœ๋Œ€ํ•œ ์ ๊ฒŒ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ๋˜๋ฉด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๊ณ  ๋””๋ฒ„๊ทธํ•  ๋•Œ๋„ ๋ฌธ์ œ๋ฅผ ์ฐพ๊ธฐ ํž˜๋“ค์–ด์ง„๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ปจํŠธ๋กค๋Ÿฌ ๋ชจ๋“  ์•ก์…˜์— ๊ณตํ†ต์ ์œผ๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์€ ์ตœ๋Œ€ํ•œ ํ•„ํ„ฐ๋กœ ์ ์šฉํ•˜๊ณ , ๋‹ค๋ฅธ ๋ถ€๋ถ„์€ ๊ฒ€ํ† ํ•ด๋ผ.

skip_xxxxx_action (์ƒ์†ํ•œ ํ•„ํ„ฐ๋ฅผ ์ œ์™ธ)

# before, after, around๋กœ ์ ์šฉ๋œ ๋ชจ๋“  ํ•„ํ„ฐ์ œ๊ฑฐ
skip_action_callback :my_logging
# ํŠน์ •ํ•œ ํ•„ํ„ฐ์ œ๊ฑฐ xxxxx๋ถ€๋ถ„์— before,after,around์ค‘ํ•˜๋‚˜!
skip_before_action :my_logging

Resource

: CRUD ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ข‹๋‹ค.

#routes.rb
resources :name [, ...]
resource :name [, ...]

resources๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” RESTful ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ, resource๋Š” ํ•˜๋‚˜์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” interface์ƒ์„ฑ! ์ด๋•Œ ์ฐจ์ด์ ์€ resource๋Š” index์•ก์…˜์ด ์ •์˜๋˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ, show,edit,delete์—์„œ :id๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.

1. constraints(๋ผ์šฐํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ œ์•ฝ์กฐ๊ฑด)

:id์˜ ๋ผ์šฐํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํฌํ•จ์ด ๋˜์–ด์žˆ์„๋•Œ ๋ชจ๋“  ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์–ด๋–ค ๊ฐ’์ด ์ „๋‹ฌ๋  ์ง€๋ฅผ ๋ฏธ๋ฆฌ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ์–ด๋Š์ •๋„ ์ œํ•œ์„ ๊ฑธ์–ด๋‘๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

# {<๋งค๊ฐœ๋ณ€์ˆ˜์ด๋ฆ„> : <์ •๊ทœํ‘œํ˜„์‹>}
resources :books, constraints: {id: /[0-9]{1,2}/}

2. ์ œ์•ฝ ํด๋ž˜์Šค ์ •์˜(๋ณต์žกํ•œ ์ œ์•ฝ์กฐ๊ฑด)

: ์ •๊ทœ ํ‘œํ˜„์‹๋งŒ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†๋Š” ๋ณต์žกํ•œ ์ œ์•ฝ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ œ์•ฝ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

#TimeConstraint.rb
class TimeConstraint
    def matches?(request)
        current = Time.now
        current.hour >= 9 && current.hour < 18
    end
end

matches?๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋ฉด๋œ๋‹ค. ์ด๋•Œ ๋‘๊ฐ€์ง€ ๊ทœ์น™์„ ์ง€์ผœ์•ผํ•œ๋‹ค.

  • ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์š”์ฒญ์ •๋ณด(request ๊ฐ์ฒด)๋ฅผ ๋ฐ›์Œ

  • ๋ฆฌํ„ด ๊ฐ’์œผ๋กœ๋Š” ๋ผ์šฐํŠธ๋ฅผ ์œ ํšจํ™” ํ• ์ง€์— ๋Œ€ํ•œ true or false ๋ฆฌํ„ด

require 'TimeConstraint'

Railbook::Application.routes.draw do
    resources :books, constraint: TimeConstraint.new
end

3. format(form ๋งค๊ฐœ๋ณ€์ˆ˜์ œ๊ฑฐ)

resources :books,format: false

URLํŒจํ„ด์—์„œ format์ด ์ œ๊ฑฐ๋œ ๋ผ์šฐํŠธ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

4. controller & as(์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค,url ํ—ฌํผ ์ด๋ฆ„์ˆ˜์ •)

resources :users, controller: :members
# UsersController๋ฅผ MembersController์™€ ๋งคํ•‘
resources :reviews, as: :comments
# reviews_path๋ฅผ comments_path์˜ ์ด๋ฆ„์œผ๋กœ ํ—ฌํผ ์ƒ์„ฑ

5. namespace & scope (๋ชจ๋“ˆ ๋‚ด๋ถ€ ์ปจํŠธ๋กค๋Ÿฌ ๋งตํ•‘)

: ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ํด๋”๋กœ ์ •๋ฆฌํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ > rails generate controller Admin::Books๋ฅผ ํ•˜๋ฉด controllers/adminํด๋” ์•„๋ž˜์— books_controller๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ์ด๋•Œ RESTful์„ ์ •์˜ํ•˜๋ ค๋ฉด

namespace :admin do
    resources :books
end

# ๋ชจ๋“ˆ์„ ์ธ์‹๋งŒ ํ•˜๊ณ  urlํŒจํ„ด๊ณผ ํ—ฌํผ์— ์˜ํ–ฅ์„ ์ฃผ๊ณ  ์‹ถ์ง€ ์•Š์€๊ฒฝ์šฐ
scope module: :admin do
    resources :books
end

6. collection & member(RESTful ์ธํ„ฐํŽ˜์ด์Šค์— ์•ก์…˜์ถ”๊ฐ€)

resources :name do
    [collection do
        method Application
        ...
    end]
    [member do
        method Application
        ...
    end]
end
# name : ๋ฆฌ์†Œ์Šค ์ด๋ฆ„
# method : ์ ์šฉํ•  HTTP ๋ฉ”์„œ๋“œ(get, post, put, patch, delete)
# action : ์ ์šฉํ•  ์•ก์…˜

7. only & except

: ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ์•ก์…˜์˜ ์ผ๋ถ€๋ฅผ ๋ฌดํšจํ™” ์‹œํ‚ค๊ณ  ์‹ถ์„ใ„ธ!

resources :users, except: [:show, :destroy]
resources :users, only: [:index, :create, :new, :update, :edit]

8. resources ์ค‘์ฒฉ

: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด๋ถ€์—์„œ ๊ณ„์ธต ๊ด€๊ณ„๋ฅผ ๊ฐ–๋Š” ๊ฒฝ์šฐ

resources :books do
    resources :reviews
end
resources :books do
    resources :reviews , shallow: true
end

shallow๋Š” :id ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์ง€ ์•Š๋Š” ์•ก์…˜์— :book_id ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋ถ™๋Š”๋‹ค.

Last updated