首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更换Rails路线助手以配合我们的意愿?

如何更换Rails路线助手以配合我们的意愿?
EN

Stack Overflow用户
提问于 2012-12-01 12:36:27
回答 1查看 338关注 0票数 0

我用以下命令生成了一个脚手架:

代码语言:javascript
复制
rails generate scaffold indice valeur:decimal date:date

我使用华丽的宝石"rails.翻译.路线“将I18国际化的URL(Path)翻译成法语。

我还将索引控制器与我的config/route.rb放在一个命名空间中:

代码语言:javascript
复制
  namespace :catalogs do
    resources :pub_indices
  end

结果的路线是不正确的,因为Rails拐点。它使用"index“代替"indice”作为单数,尽管我使用"indice“来生成并且我的模型被称为pub_indice.rb:

代码语言:javascript
复制
             catalogs_pub_indices_fr GET    /catalogues/indices(.:format)                      catalogs/pub_indices#index {:locale=>"fr"}
             catalogs_pub_indices_en GET    /en/catalogs/pub_indices(.:format)                 catalogs/pub_indices#index {:locale=>"en"}
                                     POST   /catalogues/indices(.:format)                      catalogs/pub_indices#create {:locale=>"fr"}
                                     POST   /en/catalogs/pub_indices(.:format)                 catalogs/pub_indices#create {:locale=>"en"}
           new_catalogs_pub_index_fr GET    /catalogues/indices/nouveau(.:format)              catalogs/pub_indices#new {:locale=>"fr"}
           new_catalogs_pub_index_en GET    /en/catalogs/pub_indices/new(.:format)             catalogs/pub_indices#new {:locale=>"en"}
          edit_catalogs_pub_index_fr GET    /catalogues/indices/:id/modifier(.:format)         catalogs/pub_indices#edit {:locale=>"fr"}
          edit_catalogs_pub_index_en GET    /en/catalogs/pub_indices/:id/edit(.:format)        catalogs/pub_indices#edit {:locale=>"en"}
               catalogs_pub_index_fr GET    /catalogues/indices/:id(.:format)                  catalogs/pub_indices#show {:locale=>"fr"}
               catalogs_pub_index_en GET    /en/catalogs/pub_indices/:id(.:format)             catalogs/pub_indices#show {:locale=>"en"}
                                     PUT    /catalogues/indices/:id(.:format)                  catalogs/pub_indices#update {:locale=>"fr"}
                                     PUT    /en/catalogs/pub_indices/:id(.:format)             catalogs/pub_indices#update {:locale=>"en"}
                                     DELETE /catalogues/indices/:id(.:format)                  catalogs/pub_indices#destroy {:locale=>"fr"}
                                     DELETE /en/catalogs/pub_indices/:id(.:format)             catalogs/pub_indices#destroy {:locale=>"en"}

所以我搜索了一下发现了“inflector”这个词..。因此,我在我的config/route.rb中使用了官方医生提供的技巧:

代码语言:javascript
复制
  ActiveSupport::Inflector.inflections do |inflect|
    inflect.irregular 'indice', 'indices'
  end

路线帮手看上去还不错:

代码语言:javascript
复制
             catalogs_pub_indices_fr GET    /catalogues/indices(.:format)                      catalogs/pub_indices#index {:locale=>"fr"}
             catalogs_pub_indices_en GET    /en/catalogs/pub_indices(.:format)                 catalogs/pub_indices#index {:locale=>"en"}
                                     POST   /catalogues/indices(.:format)                      catalogs/pub_indices#create {:locale=>"fr"}
                                     POST   /en/catalogs/pub_indices(.:format)                 catalogs/pub_indices#create {:locale=>"en"}
          new_catalogs_pub_indice_fr GET    /catalogues/indices/nouveau(.:format)              catalogs/pub_indices#new {:locale=>"fr"}
          new_catalogs_pub_indice_en GET    /en/catalogs/pub_indices/new(.:format)             catalogs/pub_indices#new {:locale=>"en"}
         edit_catalogs_pub_indice_fr GET    /catalogues/indices/:id/modifier(.:format)         catalogs/pub_indices#edit {:locale=>"fr"}
         edit_catalogs_pub_indice_en GET    /en/catalogs/pub_indices/:id/edit(.:format)        catalogs/pub_indices#edit {:locale=>"en"}
              catalogs_pub_indice_fr GET    /catalogues/indices/:id(.:format)                  catalogs/pub_indices#show {:locale=>"fr"}
              catalogs_pub_indice_en GET    /en/catalogs/pub_indices/:id(.:format)             catalogs/pub_indices#show {:locale=>"en"}
                                     PUT    /catalogues/indices/:id(.:format)                  catalogs/pub_indices#update {:locale=>"fr"}
                                     PUT    /en/catalogs/pub_indices/:id(.:format)             catalogs/pub_indices#update {:locale=>"en"}
                                     DELETE /catalogues/indices/:id(.:format)                  catalogs/pub_indices#destroy {:locale=>"fr"}
                                     DELETE /en/catalogs/pub_indices/:id(.:format)             catalogs/pub_indices#destroy {:locale=>"en"}

但是Rails拒绝接受像这样使用"indice“的路由:new_catalogs_pub_indice_path

相反,它接受不正确的形式:new_catalogs_pub_index_path,尽管rake路由是在说一些不同的东西。

有人能解释我做错了什么吗?(我希望Rails没有使用这种单数/复数形式。这将是如此简单的叹息)

=====更新=====

好吧,我想快点回答这个问题。经过进一步的搜索,Il意识到我的"inflector“代码应该放在config/initializers/inflections.rb中:

代码语言:javascript
复制
  ActiveSupport::Inflector.inflections do |inflect|
    inflect.irregular 'indice', 'indices'
  end

官方医生只是解释了怎么做。它不知道在哪里。因此,当上下文讨论config/route.rb时,我考虑将代码插入route.rb,但这是错误的。

官方医生对inflector缺乏解释。太可惜了,小心点吧;-)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-04 15:45:20

好的,正如我在更新中所解释的。

config/initializers/inflections.rb.中使用infelctor可以更改路由帮助程序的语法

官方医生展示了一个示例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13659513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档