【问题标题】:Rails - Multiple Routes For Many to One Relationship collectionRails - 多对一关系集合的多条路线
【发布时间】:2017-02-26 20:48:50
【问题描述】:

RubyonRails - 带有 mongoDb 的 4.x 版本。

所以我有一个区域页面作为顶级课程。它有_许多公园和房屋。 我正在尝试从同一个区域 UI 页面对公园和房屋进行 CRUD 操作。所以区域索引页面将有 2 个添加按钮(添加公园和添加房屋)。区域的索引页面还应显示所有公园和房屋,每个公园和房屋都有自己的编辑和删除按钮。

我不确定如何设置路由/控制器/视图及其文件夹结构。

class Area
   has_many :parks, dependent: :destroy
   has_many :houses, dependent: :destroy
end

class Parks
   belongs_to :area
end

class Houses
   belongs_to :area
end   

【问题讨论】:

    标签: ruby-on-rails ruby mongodb ruby-on-rails-4


    【解决方案1】:

    您可以使用基本路由配置:

    resources :areas do
      resources :parks
      resources :houses
    end
    

    问题是,通过这种配置,ParksController 和 HousesController 将与 AreasController 位于同一文件夹中。如果你想要一个名为 areas 的子目录,其中包含两个控制器,只需:

    resources :areas do
      scope module: :areas do
        resources :parks
        resources :houses
      end
    end
    

    你最终会得到这样的文件夹结构:

    app/
      controllers/
        areas/
          parks_controller.rb
          houses_controller.rb
        areas_controller.rb
      views/
        areas/
          areas/
            parks/
            houses/
          index.html.haml
          edit.html.haml
          # etc. (views for AreasController actions)
    

    【讨论】:

    • 谢谢。我将尝试使用此解决方案并报告
    猜你喜欢
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 2019-06-22
    • 2012-07-31
    • 2019-01-04
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    相关资源
    最近更新 更多