LoginSignup
3
3

More than 5 years have passed since last update.

ActiveAdminを使っていてInvalid route nameが出たときの対処法

Last updated at Posted at 2016-01-22

TL;DR

ActiveAdminを使っていたらInvalid route nameと言われてサーバーが起動しなくなったが、namespaceを文字列からシンボルにしたらとりあえずは動いた。

症状

ActiveAdminを導入するにあたって、いたって単純な管理画面を作ろうとしたら、サーバー起動時に

$ bundle exec rails s
(略)lib/ruby/gems/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:549:in `add_route': Invalid route name, already in use: 'hogehoge_admin_root'  (ArgumentError)
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: 

などと言われて困惑。さっきまでは動いてたのに!(よくある話)

hogehoge_admin/user.rb
ActiveAdmin.register User, namespace: "hogehoge_admin" do

  # See permitted parameters documentation:
  # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
  #
  # permit_params :list, :of, :attributes, :on, :model
  #
  # or
  #
  # permit_params do
  #   permitted = [:permitted, :attributes]
  #   permitted << :other if resource.something?
  #   permitted
  # end

end

hogeoge_admin以下に書いてあったのはこれだけ。
他にやったことといえば、以下のようにinitializer内でrootの変更をしたくらい。

config/initializers/active_admin.rb
config.namespace :hogehoge_admin do |hogehoge_admin|
  hogehoge_admin.root_to = 'users#index'
end

確認したこと

解決策

上にあるリンクでは再インストールしろと言われていて、嫌だなぁと思っていたところに、「namespaceをシンボルに変えてみて」と言われてのでやってみたら解決した。

hogehoge_admin/user.rb
ActiveAdmin.register User, namespace: :hogehoge_admin do

  ()

end

原因

正直よくわかってない。
一回hogehoge_admin/user.rb内でas:オプション使って、やっぱり消したりしたんだけどそれが原因だったりするんだろうか。さすがになんか違う気がするけど……。
文字列をシンボルに変えて動いたっていうことは、ルーティング自体は複数回定義されていて、シンボルだと構造的に重複が許されないだけ(?)な可能性はあるような気がしていて、後々問題が顕在化しそうな予感がして気持ちが悪い。
何かご存知の方いらっしゃいましたら教えていただけると幸いです。

3
3
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
3
3