获取集合中对象的值 select
Getting value of an object in collection select
我已经为员工创建了下拉列表。
- 我想要什么?
我想 select 他们每个人的全名。
- 表单类型:
我用simple_form。
我实际上有:
= f.input :person_id, label: "Employee", collection: @employee, prompt: "Select employee"
结果(我知道,那是参考):
在我使用 collection_select 之前,但是 simple_form 不支持对此类集合进行验证。
collection_select 的代码。这种类型的下拉列表可以正确显示全名。
= f.collection_select :person_id, @employee, :id, :fullName, {prompt: "Wybierz pracownika"}, {class: "form-control"}
更新:
fullName 是 person.rb 模型中的一个方法。
def fullName
"#{first_name} #{last_name}"
end
对象员工。
@employee = Person.where.not(type: "Client")
您按照以下代码操作:
<%= f.collection_select(:person_id, Model.all, :person_id, :fullName,{:prompt=>"Wybierz pracownika"}, {:class => 'form-control'}) %>
你将取代你的model_name。
或
= f.input :person_id, label: "Employee", collection: @employee.fullName, prompt: "Select employee"
我想对你有帮助
最简单的方法是:
= f.select :person_id, options_for_select(@employees.map{|e| [e.fullName, e.id]}), {:prompt=>"Wybierz pracownika", :required => true}
它会将全名显示为 select 选项,并将 id 分派为带有表单的值。
我已经为员工创建了下拉列表。
- 我想要什么?
我想 select 他们每个人的全名。
- 表单类型:
我用simple_form。 我实际上有:
= f.input :person_id, label: "Employee", collection: @employee, prompt: "Select employee"
结果(我知道,那是参考):
在我使用 collection_select 之前,但是 simple_form 不支持对此类集合进行验证。 collection_select 的代码。这种类型的下拉列表可以正确显示全名。
= f.collection_select :person_id, @employee, :id, :fullName, {prompt: "Wybierz pracownika"}, {class: "form-control"}
更新: fullName 是 person.rb 模型中的一个方法。
def fullName
"#{first_name} #{last_name}"
end
对象员工。
@employee = Person.where.not(type: "Client")
您按照以下代码操作:
<%= f.collection_select(:person_id, Model.all, :person_id, :fullName,{:prompt=>"Wybierz pracownika"}, {:class => 'form-control'}) %>
你将取代你的model_name。
或
= f.input :person_id, label: "Employee", collection: @employee.fullName, prompt: "Select employee"
我想对你有帮助
最简单的方法是:
= f.select :person_id, options_for_select(@employees.map{|e| [e.fullName, e.id]}), {:prompt=>"Wybierz pracownika", :required => true}
它会将全名显示为 select 选项,并将 id 分派为带有表单的值。