Rails路线:错误的单数形式表示资源 [英] Rails routes: Wrong singular for resources
问题描述
我的 routes.rb
中有以下行(Rails 4.1.4):
I have the following line in my routes.rb
(Rails 4.1.4):
resources :request_caches
但是,当我运行耙路线
我得到以下输出:
However, when I run rake routes
I get the following output:
request_caches GET /request_caches(.:format) request_caches#index
POST /request_caches(.:format) request_caches#create
new_request_cach GET /request_caches/new(.:format) request_caches#new
edit_request_cach GET /request_caches/:id/edit(.:format) request_caches#edit
request_cach GET /request_caches/:id(.:format) request_caches#show
PATCH /request_caches/:id(.:format) request_caches#update
PUT /request_caches/:id(.:format) request_caches#update
DELETE /request_caches/:id(.:format) request_caches#destroy
当你可以看到,Rails将 request_caches
的复数形式映射为 request_cach
的单数形式。但这应该是 request_cache
。由于 caches
这个词,这是否是一种特殊情况?我也玩过
As you can see, Rails somehow maps request_caches
plural to request_cach
singular. But it should be request_cache
. Is this some kind of special case, because of the word caches
? I've also played around with
resources :request_caches, as: :request_cache
但这会导致错误的路由,例如 request_cache_index
。而且,我认为这是一项标准任务,应使用Rails实习生路线帮手明确解决。
But this results in wrong routes like request_cache_index
. And furthermore, I think this is a standard task and should be solved clearly using Rails intern route helpers.
那么,我在做什么错了?
So, what am I doing wrong?
推荐答案
猜测。这不是完美的。在 config / initializers / inflections.rb
中添加
Rails guesses. It's not perfect. In config/initializers/inflections.rb
add
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'request_cache', 'request_caches'
end
You'将需要像初始化程序一样重新启动服务器。
You'll need to restart the server as it's in an initializer.
这篇关于Rails路线:错误的单数形式表示资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!