Rails Routes Wrong Prefix

时间:2015-07-09 19:49:26

标签: ruby-on-rails routes prefix

将资源重命名为“喜欢”时,我遇到了一个奇怪的错误

在routes.rb中我改变了:

resources :likes
resources :recipes do
  member do
    get :likes
  end
end

resources :loves
resources :recipes do
  member do
    get :loves
  end
end

和rake:routes说

              loves GET    /loves(.:format)                        loves#index
                    POST   /loves(.:format)                        loves#create
           new_lofe GET    /loves/new(.:format)                    loves#new
          edit_lofe GET    /loves/:id/edit(.:format)               loves#edit
               lofe GET    /loves/:id(.:format)                    loves#show
                    PATCH  /loves/:id(.:format)                    loves#update
                    PUT    /loves/:id(.:format)                    loves#update
                    DELETE /loves/:id(.:format)                    loves#destroy
       loves_recipe GET    /recipes/:id/loves(.:format)            recipes#loves
            recipes GET    /recipes(.:format)                      recipes#index
                    POST   /recipes(.:format)                      recipes#create
         new_recipe GET    /recipes/new(.:format)                  recipes#new
        edit_recipe GET    /recipes/:id/edit(.:format)             recipes#edit
             recipe GET    /recipes/:id(.:format)                  recipes#show
                    PATCH  /recipes/:id(.:format)                  recipes#update
                    PUT    /recipes/:id(.:format)                  recipes#update
                    DELETE /recipes/:id(.:format)                  recipes#destroy

当然我得到一个错误

未初始化的常量User :: Lofe

但前缀是“Lofe”?对我来说有点奇怪

1 个答案:

答案 0 :(得分:1)

这是由于rails中的主动支持多元化功能。

解决您的问题:

config/initializers/inflections.rb中的

添加以下内容:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable %w(Loves)
end

运行$ rake:routes后,我得到:

 Prefix Verb   URI Pattern               Controller#Action
loves_index GET    /loves(.:format)          loves#index
            POST   /loves(.:format)          loves#create
  new_loves GET    /loves/new(.:format)      loves#new
 edit_loves GET    /loves/:id/edit(.:format) loves#edit
      loves GET    /loves/:id(.:format)      loves#show
            PATCH  /loves/:id(.:format)      loves#update
            PUT    /loves/:id(.:format)      loves#update
            DELETE /loves/:id(.:format)      loves#destroy