1

我想在我的 Rails 应用程序中复制这个表单元素,我有一个模型,我用多个类别进行分类,我想通过 2 个选择框来处理,但我不知道如何完成它我已经尝试过了有不同的东西,但似乎无法得到它。任何人都知道如何完成此功能或教程?

这是我正在谈论的表格。特别是针对球队/球迷的。请记住,我的联想是正确的,这只是让我不知所措的观点。

http://rails-admin-tb.herokuapp.com/admin/team/new

4

2 回答 2

1

试试这个,例如

f.collection_select 'tax_ids', @taxes, :id, :name, {:name => 'line_item[tax_ids][]'}

或者

<%= collection_select :user, :role_ids, Role.find(:all, :order => 'name ASC'), :id, :name, { :selected => @user.role_ids }, { :multiple => true, :name => 'user[role_ids][]' } -%>

The :selected option should be in the last option hash, at least in recent versions of Rails. Also, if working with a form_for or fields_for object (f) you can skip the :name option, it seems to be set appropriately. There's no need for accepts_nested_attributes_for - this should work out of the box with a has_and_belongs_to_many relation. Example view code: = f.collection_select :delivery_method_ids, DeliveryMethod.inactive, :id, :to_s_with_price, {}, { :selected => @user.delivery_method_ids, :multiple => true }

于 2012-10-30T09:41:09.327 回答
0

不就是一种select_multiple吗?如果您想要的是能够对相关对象进行选择,请使用标准 collection_select ( http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select )

collection_select(:post, :fan_ids, Fan.all, :id, :name, {}, :multiple => true)

您将获得一个标准列表,您可以在其中选择 Ctrl 并单击任意数量的粉丝(或任何相关项目)

如果你想要的是 rails_admin 的实际用户界面,那么看看他们的代码就知道要写什么了。

于 2012-10-29T09:40:41.230 回答