【问题标题】:Rails Inflections and STIRails 拐点和 STI
【发布时间】:2019-11-26 03:52:33
【问题描述】:

不确定这是变形还是 STI。

我有一个模型 Activity,我有另一个类 Integrity 通过 STI 继承。在那个模型中,我有这个:

class Integrity < Activity

  def self.model_name
    Activity.model_name
  end

end

我看到这有助于在 STI 下进行路由。

我设置我的路线:

  resources :integrity, controller: :activities, type: 'Integrity'

和屈折:

   inflect.plural 'integrity', 'integrity'
   inflect.singular 'integrity', 'integrity'
   inflect.irregular 'integrity', 'integrity'

这似乎有效:

>> url_for [:new, :integrity]
=> "/integrity/new"

索引路由除外:

>> url_for [:integrity]
!! #<ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"activities", :type=>"Integrity"}, missing required keys: [:id]>

但这有效:

>> url_for [:integrity, :index]
=> "/integrity"

我遇到的问题是,对于我的其他 STI 课程的“正常”变形,我有这个:

>> url_for [:operations]
=> "/operations"

如何让 rails 将 :integrity 识别为索引路由?我希望所有 STI 类都具有相同的视图文件,然后具有用于构建动态路由等的通用结构。我不能有一些需要额外的 :index 而有些则不需要。

【问题讨论】:

    标签: ruby-on-rails sti


    【解决方案1】:

    当您将单数名称传递给 resources 时,index 路由变为 name_index_path

    在你的情况下 integrity_index_pathurl_for(:integrity, :index)

    您可以添加自定义路线

    get "integrity", to: 'activities#index', type: 'Integrity'
    resources :integrity, controller: :activities, type: 'Integrity'
    

    那你可以url_for(:integrity)

    【讨论】:

      【解决方案2】:

      我想通了 - 在问题中发布这个可能会触发答案:

      我有这个:

      resources :integrity, controller: :activities, type: 'Integrity'
      

      我应该有的时候:

      resources :integrities, controller: :activities, type: 'Integrity'
      

      还有:

      inflect.irregular 'integrity', 'integrities'
      

      我的想法是,我需要在我的应用程序中保留“普通”复数 Integrity。这消除了所有自定义路线等。

      【讨论】:

        猜你喜欢
        • 2011-09-12
        • 1970-01-01
        • 2012-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 2015-11-06
        相关资源
        最近更新 更多