首页 > 解决方案 > 带有集合选择的简单表单返回哈希

问题描述

我正在尝试根据所有者名称组织项目的下拉列表。为此,我使用 SimpleForm 和 grouped_select 方法。问题是现在它是一个返回下拉列表的哈希,而不是项目的名称。如何在此哈希中仅获取项目名称?

这就是我定义我的收藏的方式

 def option_clients_projects
        unless self.contractor.nil?
          opt_clients = self.contractor.construction_projects.map{ |cp| {:construction_project => cp.name, :client_name => cp.client.name} }
          opt_clients << {:construction_project =>"Add a new project", :client_name  => ""}
          opt_clients
        end
      end

//This is what i get out this method//

[{:construction_project=>"Toilette fr", :client_name=>"Martine depouhon"}, {:construction_project=>"démolition Chateau", :client_name=>"Carla"}]

>

这是我在 SimpleForm 中作为 grouped_select 的输入

    <%= f.input :construction_project_id, collection: current_user.option_clients_projects.group_by{ |d| d[:client_name] }, as: :grouped_select, group_method: :last, group_label_method: :first %>

//The result of the group_by operation//

 {"Martine depouhon"=>[{:construction_project=>"Toilette fr", :client_name=>"Martine depouhon"}], "Carla "=>[{:construction_project=>"démolition Chateau 2 2", :client_name=>"Carla "}]}
    >

我的代码的视觉结果

group_method - 方法的名称,当在集合的成员上调用该方法时,会返回代表标签的子对象数组。

标签: ruby-on-railsgroup-bysimple-formsimple-form-forgrouped-collection-select

解决方案


推荐阅读