0

我的订单/新页面中有这样的表格。我用它来选择模型位置,然后根据上面选择框中选择的内容从该位置选择一个模型。

<%= f.label "SELECT MODEL LOCATION *" %>
          <div class="list_number">1</div><%= f.collection_select :location_id, Location.all, :id, :formatted_display, prompt: true, :required => true,  :class => 'chosen-select' %>

          <%= f.label "SELECT YOUR MODEL *" %>
          <div class="list_number">2</div>
          <%= f.grouped_collection_select :performer_id, Location.order(:name).map{|group| Performer.find_by_location_id(group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, :performers, :name, :id, :first_name, include_blank: true, :required => true,  class: 'chosen-select' %>

在 grouped_collection_select 中,当我使用 map 设置数据属性时,出现以下错误:

undefined method `map' for #<Performer:0x00000006813958>

如何解决此错误并获取所需数据?


尝试以下代码后

<%= f.grouped_collection_select :performer_id, Location.order(:name).map{|group| Performer.where(location_id: group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, :performers, :name, :id, :first_name, include_blank: true, :required => true,  class: 'chosen-select' %>

我收到以下错误

undefined method `performers' for [{"data-markup"=>#<BigDecimal:67e80c8,'0.0',9(36)>}]:Array

当我尝试以下

<%= f.grouped_collection_select :performer_id, Location.order(:name), :performers, :name, :id, :first_name,Location.order(:name).map{|group| Performer.where(location_id: group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, include_blank: true, :required => true,  class: 'chosen-select' %>

我收到以下错误

undefined method `merge' for #<Array:0x000000069705a8>
4

1 回答 1

0
Performer.find_by_location_id(group.id)

返回一个实例,但map它是 Array 的方法。修复取决于您的业务逻辑(和 APP 结构)Performer.where(location_id: group.id).map...如果有很多执行者,您可以使用

我认为您尝试错误地使用此助手。根据文档,您应该通过 acollectiongroup_method.group_method - The name of a method which, when called on a member of collection, returns an array of child objects representing the <option> tags.

您将对象数组[{'data-markup' => some_value}, ...]作为集合和performersgroup_method 传递。Rails 尝试调用{'data-markup' => some_value}.performers,这会引发异常。

您应该按以下方式重写集合: 1. 它应该是数组 2. 数组的每个项都应该有方法performers- 它返回一个组项下的对象数组

我希望它会帮助你

我无法测试,但它可以工作(如果 Location has_many performers 或者您可以根据您的业务逻辑实现自己的方法)

<%= f.grouped_collection_select :performer_id, Location.order(:name), :performers, :name, :id, :first_name, include_blank: true, :required => true,  class: 'chosen-select' %>
于 2013-10-08T18:43:26.637 回答