18287


システムをRails3.2最新からRails4.0最新にアップグレードしました。そこでRailsを起動するとroutesの設定でエラーが出るようになりました。↓エラー内容

[code]

    [hoge@korekore 4.0_app]$ rails s
    => Booting WEBrick
    => Rails 4.0.13 application starting in development on http://0.0.0.0:3000
    => Run `rails server -h` for more startup options
    => Ctrl-C to shutdown server
    Exiting
    /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/route_set.rb:430:in `add_route': Invalid route name, already in use: 'apl_nw_login'  (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:
    http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:1484:in `add_route'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:1461:in `decomposed_match'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:1442:in `block in match'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:1433:in `each'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:1433:in `match'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:626:in `map_method'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:595:in `post'
            from /hogehoge/workspace/4.0_app/config/routes.rb:34:in `block (4 levels) in <top (required)>'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:877:in `block in constraints'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:753:in `scope'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:877:in `constraints'
            from /hogehoge/workspace/4.0_app/config/routes.rb:31:in `block (3 levels) in <top (required)>'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:753:in `scope'
            from /hogehoge/workspace/4.0_app/config/routes.rb:30:in `block (2 levels) in <top (required)>'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:818:in `block in namespace'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:753:in `scope'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:818:in `namespace'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/mapper.rb:1394:in `namespace'
            from /hogehoge/workspace/4.0_app/config/routes.rb:29:in `block in <top (required)>'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/route_set.rb:337:in `instance_exec'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/route_set.rb:337:in `eval_block'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/actionpack-4.0.13/lib/action_dispatch/routing/route_set.rb:315:in `draw'
            from /hogehoge/workspace/4.0_app/config/routes.rb:16:in `<top (required)>'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:223:in `load'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:223:in `block in load'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:214:in `load_dependency'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:223:in `load'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/application/routes_reloader.rb:40:in `each'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/application/routes_reloader.rb:40:in `load_paths'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/application/routes_reloader.rb:16:in `reload!'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/application/routes_reloader.rb:26:in `block in updater'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/activesupport-4.0.13/lib/active_support/file_update_checker.rb:75:in `call'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/activesupport-4.0.13/lib/active_support/file_update_checker.rb:75:in `execute'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/application/routes_reloader.rb:27:in `updater'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/application/routes_reloader.rb:6:in `execute_if_updated'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/initializable.rb:30:in `instance_exec'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/initializable.rb:30:in `run'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/initializable.rb:55:in `block in run_initializers'
            from /hogehoge/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
            from /hogehoge/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
            from /hogehoge/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
            from /hogehoge/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
            from /hogehoge/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `each'
            from /hogehoge/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `call'
            from /hogehoge/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
            from /hogehoge/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
            from /hogehoge/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/initializable.rb:54:in `run_initializers'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/application.rb:215:in `initialize!'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
            from /hogehoge/workspace/4.0_app/config/environment.rb:5:in `<top (required)>'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:229:in `require'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:229:in `block in require'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:214:in `load_dependency'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:229:in `require'
            from /hogehoge/workspace/4.0_app/config.ru:3:in `block in <main>'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/rack-1.5.5/lib/rack/builder.rb:55:in `instance_eval'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/rack-1.5.5/lib/rack/builder.rb:55:in `initialize'
            from /hogehoge/workspace/4.0_app/config.ru:in `new'
            from /hogehoge/workspace/4.0_app/config.ru:in `<main>'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/rack-1.5.5/lib/rack/builder.rb:49:in `eval'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/rack-1.5.5/lib/rack/builder.rb:49:in `new_from_string'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/rack-1.5.5/lib/rack/builder.rb:40:in `parse_file'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/rack-1.5.5/lib/rack/server.rb:277:in `build_app_and_options_from_config'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/rack-1.5.5/lib/rack/server.rb:199:in `app'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/commands/server.rb:48:in `app'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/rack-1.5.5/lib/rack/server.rb:314:in `wrapped_app'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/commands/server.rb:75:in `start'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/commands.rb:76:in `block in <top (required)>'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/commands.rb:71:in `tap'
            from /hogehoge/.rbenv/versions/2.2.3/gemsets/4.0rails/gems/railties-4.0.13/lib/rails/commands.rb:71:in `<top (required)>'
            from bin/rails:4:in `require'
            from bin/rails:4:in `<main>'
    
[/code]

実装config/routes.rbでscopeを分けてログイン画面を分けています。  
エラーの原因はas =>
:nw_loginが被ってるせいだとは分かっているのですが、できればnw_login等の名前を変えずに対応したいと思っています。何か対応方法はありますでしょうか？

[code]

     namespace :apl, :path => "/a" do
       scope :module => :adm do
         get "login" => "login#index", :as => :nw_login
         post "login" => "login#login", :as => :login
       end
     end
    
     namespace :apl, :path => "/a" do
       scope ":name", :module => :manage do
         get "login" => "login#index", :as => :nw_login
         post "login" => "login#login", :as => :login
    
[/code]

Rubyバージョンは2.2.3  
Pathは↓の感じで出ることを期待してます。Rails3.2では出てました。

[code]

    2015/10/30 14:17:34 [INFO] :: Connecting to database specified by database.yml
                                      apl_nw_login GET    /a/login(.:format)                                                                                  apl/adm/login#index
                                      apl_nw_login GET    /a/:name/login(.:format)                                                                     apl/manage/login#index
                                         apl_login POST   /a/login(.:format)                                                                                  apl/adm/login#login
                                          apl_login POST   /a/:name/login(.:format)                                                                     apl/manage/login#login
    
[/code]


CC BY-SA 3.0
2015-10-30T03:14:06.857
18234
2015-10-31T23:12:35.173
2015-10-30T05:34:12.473
10851
10851
question
ruby-on-rails ruby
Rails3.2からRails4.0に上げたらroutingでArgumentErrorが出る


「エラーの原因はas =>
:nw_loginが被ってるせいだとは分かっているのですが、できればnw_login等の名前を変えずに対応したい」とのことですが、Rails
4で仕様が変わったのでそれは無理だと思います。

Railsのメジャーバージョンアップでは多かれ少なかれ既存のコードを修正する必要が出てくるので、諦めて新しいパス名を付けて、既存のコードを修正してください。

参考までに修正例を載せておきます。

[code]

    # routes.rb
    namespace :apl, :path => "/a" do
       scope :module => :adm do
         get "login" => "login#index", :as => :nw_login
         post "login" => "login#login", :as => :login
       end
     end
    
     namespace :apl, :path => "/a" do
       scope ":name", :module => :manage do
         get "login" => "login#index", :as => :name_nw_login
         post "login" => "login#login", :as => :name_login
    
     # view
     <%= link_to 'ログイン', apl_name_nw_login_path %>
    
[/code]


CC BY-SA 3.0
2015-10-31T23:12:35.173
18287
2015-10-31T23:12:35.173
85
18234
answer