Rails 路线前缀错误
Rails Routes Wrong Prefix
将资源 "like" 重命名为 "love"
时出现奇怪的错误
在 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" 是从哪里来的呢?我觉得有点奇怪
这是由于 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
将资源 "like" 重命名为 "love"
时出现奇怪的错误在 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" 是从哪里来的呢?我觉得有点奇怪
这是由于 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