티스토리 뷰
routes.rb 에서 nested 라우팅 관리를 하다보면 아래와 같은 경우가 종종 생긴다.
resources :messaged do
resources :comments
resources :categories
resources :tags
end
resources :posts do
resources :comments
resources :categories
resources :tags
end
resources :items do
resources :comments
resources :categories
resources :tags
end
comments, categories, tags 와 같은 똑같은 resources 가 중복되어 사용되는 경우 concern method를 이용하여 간단히 나타낼 수 있다.
concern :sociable do
resources :comments
resources :categories
resources :tags
end
resources :messages, concerns: :sociable
resources :posts, concerns: :sociable
resources :items, concerns: :sociable
특정 액션을 제한하고 싶으면 options 인자를 이용하면 된다.
concern :sociable do |options|
resources :comments, options
resources :categories, options
resources :tags, options
end
resources :messages, concerns: :sociable
resources :posts, concerns: :sociable
resources :items do
concerns: :sociable, only: :create
end
concern 정의를 파일에 별도로 뽑아 저장할 수 있다.
# app/concerns/sociable.rb
class Sociable
def self.call(mapper, options)
mapper.resources :comments, options
mapper.resources :categories, options
mapper.resources :tags, options
end
end
# config/routes.rb
concern :sociable, Sociable
resources :messages, concerns: :sociable
resources :posts, concerns: :sociable
resources :items do
concerns: :sociable, only: :create
end
'Ruby&Rails > Rails' 카테고리의 다른 글
| [Rails] 웹 API를 위한 JSON, XML 응답 (0) | 2015.07.07 |
|---|---|
| [Rails] Rails에서의 subdomain 설정하기 (0) | 2015.07.06 |
| [Rails] Ajax로 Create & Delete 기능 구현 (0) | 2015.06.26 |
| [Rails] N+1 쿼리 문제 해결방안 (0) | 2015.06.25 |
| [Rails] Redirect and Flash 문법 (0) | 2015.06.24 |
- Total
- Today
- Yesterday
- rabbitmq
- SK하이닉스
- Java
- 한미반도체
- ruby on rails
- CSS
- 프로그래밍
- 웹프로그래밍
- 이펙티브 자바
- 티스토리 초대장
- javascript
- 알고리즘
- 주식투자
- Rails
- OpenStack
- 이수페타시스
- ruby
- 국제유가
- 투자전략
- ubuntu
- 반도체관련주
- 현대차
- codecademy
- 엔비디아
- Message Queue
- install
- 자료구조
- 삼성전자
- HBM
- html
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
