Rails 路线:资源的单数错误 [英] Rails routes: Wrong singular for resources

查看:10
本文介绍了Rails 路线:资源的单数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 routes.rb (Rails 4.1.4) 中有以下行:

I have the following line in my routes.rb (Rails 4.1.4):

resources :request_caches

但是,当我运行 rake routes 时,我得到以下输出:

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.

那么,我做错了什么?

推荐答案

Rails 猜测.这并不完美.在 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'll need to restart the server as it's in an initializer.

这篇关于Rails 路线:资源的单数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆