ruby-on-rails - 带有集合选择的简单表单返回哈希
问题描述
我正在尝试根据所有者名称组织项目的下拉列表。为此,我使用 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 - 方法的名称,当在集合的成员上调用该方法时,会返回代表标签的子对象数组。
解决方案
推荐阅读
- java - 如何在java中每秒运行60次代码
- node.js - 在我的 slackbot 应用程序中,函数被节点执行两次
- javascript - 如何让全局变量留在角度构造函数和承诺中,调用需要来自 diff 组件的数据的函数
- javascript - 自定义设置方法不起作用(javascript)
- android - Android 运行时错误:芯片膨胀期间发生的 ID 0x00000001 无效
- apache-spark-sql - 在 Spark SQL 中使用字符串变量作为日期过滤观察结果
- android - 我必须有一个结算帐户才能使用 Google Maps Api 密钥吗?
- python - 有没有办法在 Django 模型中拥有一个列表类型的对象?
- python - 随机化项目和循环列表(Python)
- java - 在另一个函数的参数中实现的函数的android java语法是什么