LoginSignup
1
5

More than 5 years have passed since last update.

名前空間について

Last updated at Posted at 2019-04-04

名前空間とは

名前空間とは、モジュールです。
モジュールというのは、ざっくりいうと「部品」です。
ひとつのモデルのなかに、いくつかのクラスを設定できるのですが、例えば、学校でいうと、教室ですね。

管理系のモジュールとして 「Admin」を設定します。
そして「Admin」クラスに生徒がたくさんいるわけですね。

今回は、Admin専用のcontrollerを作ります。
controllerが生徒です。

具体的なパスとしては、

アプリ名/app/controllers/admin/users_controller.rb

です。

同じ名前のcontorollerが上位の階層にいますが、その子は年長クラスの同一同名の別人です。
このように、Railsではモジュール階層がディレクトリに対応していますから、分かりやすいですね。

名前空間を設定すると、一つのモデルの中で、幾つかのCRUD処理を行うことができますし、機能毎に実装追加ができますので、コードが複雑化しません。

名前空間を作ってみよう!

普通に、ディレクトリを作って、直下に手動でcontrollerを作っても良いです。
以下のコマンドでも作られますよ。

bin/rails generate controller Admin::Users 

controllerの表記も少し変わります。

app/controllers/admin/users_controller.rb
class Admin::UsersController < ApplicationController

end

ルートの設定をするときは、

routes.rb
Rails.application.routes.draw do
  namespace :admin do
    resources :users
  end
end

resourcesを使うかどうかは、お好みだと思いますが、楽ですよ。
以下に、ターミナルのルーティングを載せます。
まずはターミナルで以下のコマンドを入力してください。

bin/rails routes

すると、

                   Prefix Verb   URI Pattern                                                                            
                                          Controller#Action
              admin_users GET    /admin/users(.:format)                                                                   
                                          admin/users#index
                          POST   /admin/users(.:format)                                                                   
                                          admin/users#create
           new_admin_user GET    /admin/users/new(.:format)                                                               
                                          admin/users#new
          edit_admin_user GET    /admin/users/:id/edit(.:format)                                                          
                                          admin/users#edit
               admin_user GET    /admin/users/:id(.:format)                                                               
                                          admin/users#show
                          PATCH  /admin/users/:id(.:format)                                                               
                                          admin/users#update
                          PUT    /admin/users/:id(.:format)                                                               
                                          admin/users#update
                          DELETE /admin/users/:id(.:format)                                                               
                                          admin/users#destroy

これだけのルーティングが瞬時に終わるので、resourcesは使った方がいいんじゃないかな、と思います。
ただし、どのようにしてパラメータの受け渡しなどが行われるのかは、絶対にわかっておいた方が良いと思います。

1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5