【问题标题】:Display selected option for an associated collection_select field from within a nested_form_field从nested_form_field 中显示相关collection_select 字段的选定选项
【发布时间】:2015-11-03 17:34:28
【问题描述】:

这是我的关联:

class Agency < ActiveRecord::Base
  has_many :agencies_hotline_services, dependent: :destroy
  has_many :hotline_services, through: :agencies_hotline_services 
end

#rich join table between agencies and hotline_services
class AgenciesHotlineService < ActiveRecord::Base
  belongs_to :hotline_service
  belongs_to :agency
end

class HotlineService < ActiveRecord::Base
  belongs_to :hotline_service_category
  has_many :agencies_hotline_services
  has_many :agencies, through: :agencies_hotline_services
end

class HotlineServiceCategory < ActiveRecord::Base
  has_many :hotline_services
end

当用户创建一个新的agency 时,我使用nested_form_fields gem,以便用户可以为agency 动态添加新的hotline_services

当点击添加该机构提供的服务按钮时:

.

当用户从该选择框中选择hotline_service_category 时,将调用一个ajax 请求,然后将hotline_service 选择更新为仅与所选hotline_service_category 关联的hotline_services

在创建 new agency 时,这一切都非常有用。我遇到麻烦的地方是当我想编辑现有的agency:

agency 表单的nested_fields 部分正确显示了每个代理机构关联的hotline_services 的现有选定选项。但是,表单显示每个hotline_service 的关联hotline_service_category 选项选择。我确实想显示 hotline_service_category 的现有选定选项。

注意hotline_service_category 未保存在富连接表中。通过关联的hotline_service 获取它。

因此,不知何故,我的集合选择为hotline_service_category,我想说:嘿,查看位于此选项下方 collection_select 中的选定hotline_service 选项,获取关联的hotline_service_category,并将其显示为hotline_service_category 的选定选项。

这是来自_form.html.erbagency 的相关代码:

<%= f.nested_fields_for :agencies_hotline_services do |ff| %>
  <div class="row">
    <div class="col-sm-4 form-group">
      <%= label_tag "Service Category" %>
      <%= collection_select(:hotline_service_category_id, nil, HotlineServiceCategory.all, :id, :description, {prompt: '-Select-'}, class: "form-control", id: "new_edit_hotline_service_category_selection") %>
    </div>
    <div class="col-sm-5 form-group">
      <%= ff.label "Service Provided" %>
      <%= ff.collection_select(:hotline_service_id, HotlineService.all, :id, :description, {}, class: "form-control hotline_service_selection") %>
    </div>  
    <div class="col-sm-3">
      <br>
      <%= ff.remove_nested_fields_link "Remove", class: "btn btn-small btn-danger "%>
    </div>
  </div>
<% end %>
<%= f.add_nested_fields_link :agencies_hotline_services, "Add a Service this Agency Provides", class: "btn btn-primary" %>

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    hotline_service_category 没有存储在数据库中(它不是嵌套表单“知道”的属性的一部分),这就是它不自动设置它的原因。

    HotlineService belongs_to :hotline_service_category 所以hotline_service 将在hotline_service_category_id 的数据库中有一个字段

    所以你可以做的是检查是否有hotline_service,如果它存在,取hotline_service_category_id并将collection_select选择的值设置为hotline_service_category_id

    所以你需要做的是像这样添加selected 值:

    <%= collection_select(:hotline_service_category_id, .... {selected: ID} ) %>
    

    你可以像这样得到嵌套对象:ff.object 类型为agencies_hotline_services。 然后您可以使用以下方式获取类别ID:ff.object.hotline_service.hotline_service_category_id

    您应该使用try 来避免零错误。所以:

    ff.object.try(:hotline_service).try(:hotline_service_category_id)
    

    但是你应该首先检查记录是否不是新的,因为如果它是新的,你就没有设置一个选定的值。你可以通过检查来做到这一点:

    ff.object.new_record?
    

    让我们把所有东西结合起来,你会得到:

    !ff.object.new_record? && ff.object.try(:hotline_service).try(:hotline_service_category_id) ? {} : {selected: ff.object.try(:hotline_service).try(:hotline_service_category_id) } 
    

    将此添加到您的 collection_select 字段,它应该设置值。

    更简洁的方法是使用其他变量来存储它,这样集合选择就不会那么长。

    <% selected_value = ff.object.try(:hotline_service).try(:hotline_service_category_id) if !ff.object.new_record? %>
    

    然后添加:

    selected_value ? {} : {selected: selected_value } 
    

    *** 我不确定“selected:ID”是设置选择值的方式,也许你需要类别的名称或类别在集合中的位置,但我确定你会设法自己找到它。

    编辑:

    我使用枚举作为 collection_select。 我可以使用它显示的字符串来设置集合的选定值。 (在我的情况下:模型的名称) 所以你可以试试:

    ff.object.try(:hotline_service).try(:hotline_service_category).try(:description)
    

    或任何包含显示的类别文本的字段名称。

    【讨论】:

      猜你喜欢
      • 2019-11-30
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 2016-07-06
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      相关资源
      最近更新 更多