36
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails4でルーティングを分割する

36
Last updated at Posted at 2014-09-29

概要

Rails4でAPIとWebなどでルーティングを分割したいときの方法。
いろいろ調べてもうまく行かなかったのでソースをあさってみた。

方法

1. routingファイルを置く

特に指定はありませんが、config/routesというディレクトリを作るとわかりやすいでしょう。

config/routes/admin.rb
config/routes/api/v1.rb
config/routes/api/v2.rb
...
config/routes/admin.rb

Rails.application.routes.draw do

  # こんな感じでここに設定を書く
  scope module: 'admin' do

    get 'welcome/index'
    ...

  end

end

2. 設定ファイルを変更する

config/application.rb

module YourApplication
  class Application < Rails::Application

    config.paths["config/routes.rb"].concat Dir[Rails.root.join("config/routes/**/*.rb")]
    # << とか += ではうまくいかない。。

  end
end

まとめ

うまくいったら「ストック」してね

参考

36
33
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
36
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?