【问题标题】:What is the best way to add inflections in a Rails plugin?在 Rails 插件中添加变形的最佳方法是什么?
【发布时间】:2009-05-06 12:21:18
【问题描述】:

我正在编写一个插件,它需要一些它自己的变形。使用嵌入了引擎的 rails 2.3,我应该在哪里放置我的拐点?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-plugins


    【解决方案1】:

    我建议在您的插件 lib 目录插件中添加一个单独的文件 (inflections.rb)。通过在插件 Ruby 文件的开头添加以下内容,您应该能够从插件加载 inflections.rb 文件。

    require 'inflections"
    

    您的 inflections.rb 文件应遵循新 Rails 项目中作为示例提供的格式:

    # Sample Inflections    
    # ActiveSupport::Inflector.inflections do |inflect|
    #   inflect.plural /^(ox)$/i, '\1en'
    #   inflect.singular /^(ox)en/i, '\1'
    #   inflect.irregular 'person', 'people'
    #   inflect.uncountable %w( fish sheep )
    # end
    

    【讨论】:

      【解决方案2】:

      更新:对于遇到相同问题但使用 Rails 5 的任何人,这是正确的答案。

      不要将inflections.rb 文件放在plugin/lib 文件夹中,而是放在plugin/config/initializers 文件夹中。

      然后简单地写下你的变形(例如陈词滥调/陈词滥调):

      ActiveSupport::Inflector.inflections do |inflect|
          inflect.irregular 'cliche', 'cliches'
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-21
        • 1970-01-01
        • 2013-08-21
        • 1970-01-01
        • 2019-05-13
        • 2017-12-28
        • 2017-09-16
        • 1970-01-01
        相关资源
        最近更新 更多