【问题标题】:Display objects that belong to parent object dynamically in rails 4在rails 4中动态显示属于父对象的对象
【发布时间】:2014-07-18 15:33:05
【问题描述】:

我正在尝试创建基于选定父对象的动态下拉列表。

我的模型是社区、地段、住宅和社区住宅

class Community < ActiveRecord::Base
  has_many :lots
  has_many :community_homes
  has_many :communities, through: :community_homes
end
------------------------------------------------------------------
class CommunityHome < ActiveRecord::Base
  belongs_to :community
  belongs_to :home
end
------------------------------------------------------------------
class Lot < ActiveRecord::Base
  belongs_to :community
end
------------------------------------------------------------------
class Home < ActiveRecord::Base
  has_many :community_homes
  has_many :communities, through: :community_homes
end
-------------------------------------------------------------------

表单有一个包含所有社区的下拉列表。我需要其他下拉列表来仅显示与所选社区相关联的地段和房屋。

例如,我的一个社区有 29 个地块和 14 个平面图(房屋)。选择该社区时,我希望这29个批次和14所房屋成为下拉列表中唯一的对象,而不是所有198个批次和45个家庭。任何帮助和/或正确方向的简单点将不胜感激......谢谢!

我选择社区、地段和住宅的形式是合同。 Homes CRUD 独立于社区和合同,然后通过关联通过 has_many 加入社区。地段被创建为属于社区。

class Contract < ActiveRecord::Base
  validates :customer_name, :customer_phone_number, presence: true
  has_one :community
  has_one :lot
  has_one :home
end

合同表格

= simple_form_for @contract do |f|
  .community-select
    = f.collection_select( :community, Community.all, :id, :name, {}, { :multiple => false } )
  %br
  .lot-select  
    = f.collection_select( :lot_number, Lot.all, :id, :number, {}, { :multiple => false } )
  %br
  .inputs
    = f.text_field :property_address, placeholder: "Property Address", class: "myform-control"
  %br
    = f.text_field :customer_name, placeholder: "Customer Name", class: "myform-control"
  %br
    = f.text_field :customer_phone_number, placeholder: "Customer Phone Number", class: "myform-control"
  %br
    = f.text_field :selection_date, placeholder: "Selection Date", class: "myform-control"
  %br
  .home-select
    = f.collection_select(:home, Home.all, :name, :name, {}, {:multiple => false } )
  %br
= f.submit 'Save', class: "btn btn-success"
= link_to "Cancel", contracts_path, type: "button", class: "btn btn-danger"

我正在为 .json 使用 gem active_model_serializer

【问题讨论】:

    标签: javascript jquery ruby-on-rails ajax json


    【解决方案1】:

    我会(因为它已经应该)将下拉选项的值设为community 的 id。使用 JS 在该元素上添加一个事件侦听器,当它发生变化时,使用选定的 community's id 进行 AJAX 调用以获取下一条信息。然后在 AJAX 调用的响应处理程序中填充选项。这对于任何 JS MV* 框架(例如骨干、ember、angular 等)来说都是一个很好的用例。然后,您可以使用模板框架生成要插入 DOM 的 HTML。

    【讨论】:

      【解决方案2】:

      这就是我最终让它工作的方式。我摆脱了 active_model 序列化程序 gem,并像在 Rails 中一样使用 JSON。
      我的路线.rb

      Rails.application.routes.draw do
      ...
        resources :communities, only: [:index, :show] do
          get :lots, on: :member
          get :homes, on: :member
        end
      ...
      

      communities_controller.rb

      class CommunitiesController < ApplicationController
      
        def lots
          @lots = @community.lots.available
          respond_to do |format|
            format.html
            format.json { render json: @lots }
          end
        end
      
        def homes
          @homes = @community.homes.all
            respond_to do |format|
            format.json { render json: @homes }
          end
        end
      

      contracts.js.coffee

        $("#contract_community_id").on "change", ->
          id = $(this).val()
          $.getJSON "/communities/#{id}/lots", (lot) ->
            $.each lot, (key, value) ->
              options = ""
              i = 0
              number = this.number
              while i < lot.length
                options += "<option value=\"" + lot[i].number + "\">" + lot[i].number + " "</option>"
                i++
              $("#contract_lot_number").html options
              return
            return
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-13
        • 2023-04-08
        • 2011-03-30
        • 2012-05-21
        • 2022-01-05
        • 1970-01-01
        • 2016-06-23
        相关资源
        最近更新 更多