简体   繁体   English

Quiz模型的Rails布线

[英]Rails routing for Quiz model

So i've looked around and don't see a built in way to handle words that are named weirdly in the english language. 因此,我四处张望,没有看到一种内置的方式来处理英语中怪异的单词。

I have a Quiz model, which means that i have an index action of \\quizes. 我有一个测验模型,这意味着我有一个\\ quizes的索引动作。

resources 'quizes', only: ['index', 'new', 'show']  

I would like to be able to do things like 我希望能够做类似的事情

quizes_path #Note this one works
quiz_path(:id) #this does not

I have to do quize_path(:id) 我必须做quize_path(:id)

Here is the rake routes, notice that the path names are quize instead of quiz. 这是瑞克路线,请注意,路径名称是测验而不是测验。

              quizes GET    /quizes(.:format)              quizes#index
           new_quize GET    /quizes/new(.:format)          quizes#new
               quize GET    /quizes/:id(.:format)          quizes#show

This is more for learning. 这更多地是为了学习。 I know i could just manually specify the singular resources and be done. 我知道我可以手动指定单个资源并完成。

The ultimate question, is there a way to specify this in resources so that it knows that the singluar resources should remove 'es' instead of just 's' 最终的问题是,是否有一种方法可以在资源中指定它,以便它知道单个资源应删除“ es”而不是“ s”

EDIT 编辑

Here is the correct inflector based on the resources. 这是基于资源的正确偏转器。 Thanks! 谢谢!

ActiveSupport::Inflector.inflections(:en) do |inflect| ActiveSupport :: Inflector.inflections(:en)做| inflect | inflect.singular /^(quiz)es/i, '\\1' end inflect.singular / ^(quiz)es / i,'\\ 1'end

Use quizzes instead of quizes in your route definition: 在路线定义中使用quizzes代替quizes

resources :quizzes

Then the singular routes are: 那么奇异的路线是:

    new_quiz GET    /quizzes/new(.:format)       quizzes#new
   edit_quiz GET    /quizzes/:id/edit(.:format)  quizzes#edit
        quiz GET    /quizzes/:id(.:format)       quizzes#show

As @Anand's answer says you need to spell "quizzes" correctly in this instance. 正如@Anand的回答所说,在这种情况下,您需要正确拼写“测验”。 To answer your general question you're looking for inflections in the file config/initializers/inflections.rb . 为了回答您的一般问题,您正在文件config/initializers/inflections.rb寻找config/initializers/inflections.rb This has been asked before: Ruby on Rails Inflection Issue and Ruby on Rails: How do you explicitly define plural names and singular names in Rails? 之前有人问过这个问题: Ruby on Rails Inflection IssueRuby on Rails:如何在Rails中明确定义复数名称和单数名称? as examples. 作为例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM