Help us understand the problem. What is going on with this article?

concernを使ったおなじみルートの追加

More than 5 years have passed since last update.

確認画面とか、いろんなページで同じルートを設定する場合に便利。

例えばこんな感じで確認画面のルートを設定していた場合、

routes.rb
resources :users do
  patch :confirm, :on => :member
  put   :confirm, :on => :member
  post  :confirm, :on => :collection
end

resources :information do
  patch :confirm, :on => :member
  put   :confirm, :on => :member
  post  :confirm, :on => :collection
end

concernを使うとこんなにスッキリ!

routes.rb
concern :confirmable do
  patch :confirm, :on => :member
  put   :confirm, :on => :member
  post  :confirm, :on => :collection
end

resources :users, :concerns => :confirmable
resources :information, :concerns => :confirmable

こうすると、 rake routes ではこんな感じ。

$ rake routes | grep "users\|information"
                  confirm_user PATCH    /users/:id/confirm(.:format)       users#confirm
                                 PUT    /users/:id/confirm(.:format)       users#confirm
                   confirm_users POST   /users/confirm(.:format)           users#confirm
                           users GET    /users(.:format)                   users#index
                                 POST   /users(.:format)                   users#create
                        new_user GET    /users/new(.:format)               users#new
                       edit_user GET    /users/:id/edit(.:format)          users#edit
                            user GET    /users/:id(.:format)               users#show
                                 PATCH  /users/:id(.:format)               users#update
                                 PUT    /users/:id(.:format)               users#update
                                 DELETE /users/:id(.:format)               users#destroy
      confirm_system_information PATCH  /information/:id/confirm(.:format) information#confirm
                                 PUT    /information/:id/confirm(.:format) information#confirm
confirm_system_information_index POST   /information/confirm(.:format)     information#confirm
        system_information_index GET    /information(.:format)             information#index
                                 POST   /information(.:format)             information#create
          new_system_information GET    /information/new(.:format)         information#new
         edit_system_information GET    /information/:id/edit(.:format)    information#edit
              system_information GET    /information/:id(.:format)         information#show
                                 PATCH  /information/:id(.:format)         information#update
                                 PUT    /information/:id(.:format)         information#update
                                 DELETE /information/:id(.:format)         information#destroy

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした