在用户模型上,我的一个字段有多个复选框。复选框选择当前存储为数组。我正在尝试为用户在Administrate仪表板的编辑视图中重新创建多个复选框,但不确定如何创建(文档中的选项似乎没有涉及到这一点)。
下面是介绍该字段的迁移:
class AddEmploymentTypesToUser < ActiveRecord::Migration[6.0]
def change
add_column :users, :employment_types, :text, array:true, default: []
end
end我要在仪表板编辑视图中重新创建的用户表单中的UI:

该表单的代码:
<div class="form-group">
<label class="font-weight-bold" for="">What type of employment are you looking for?</label>
<br>
<%= f.check_box :employment_types, { :multiple => true }, 'Full-time', nil%> Full-time
<br>
<%= f.check_box :employment_types, { :multiple => true }, 'Part-time', nil%> Part-time
<br>
<%= f.check_box :employment_types, { :multiple => true }, 'Freelance', nil%> Freelance
<br>
<%= f.check_box :employment_types, { :multiple => true }, 'Contract-to-hire', nil%> Contract-to-hire
</div>发布于 2020-01-07 20:59:09
您必须为此提供一个自定义字段类型,因为Administrate提供的字段类型在这里并不好用。我很确定这样做的原因是这类字段可以以多种方式实现,因此Administrate不能提供单一的、统一的解决方案。
要创建新字段,请从Administrate::Field::Base继承
### app/fields/checkbox_list.rb
class CheckboxList < Administrate::Field::Base
def self.permitted_attribute(attr, _options = nil)
# Yes, this has to be a hash rocket `=>`,
# not a colon `:`. Otherwise it will be the key
# `attr` (literally) as opposed to a key whose name
# is the value of the argument `attr`.
{ attr => [] }
end
def choices
options[:choices]
end
end对于您的特定情况,我将实现两个方法。我将逐一解释它们。
首先是self.permitted_attribute。这是一个内部使用的应用程序接口,用于确定如何将新字段类型转换为params.require(...).permit(...)术语。
因为您的字段被建模为复选框列表,所以params会将其视为一个数组:
params[:user]
# => { name: "Conway Anderson", employment_types: ["Freelance", "Contract-to-hire"] }要让permit接受这一点,通常需要在Rails应用程序中执行以下操作:
params.require(:user).permit(:name, employment_types: [])通过像我上面所做的那样实现CheckboxList.permitted_attributes,Administrate将正确的信息(employment_types: [])传递给permit:它表示允许employment_types,它将是一个数组值。您可能已经在其他地方的应用程序中执行此操作了?
这是针对第一种方法的!现在是第二个:choices。这是从options读取的,它是在仪表板定义中提供给字段的选项列表。下面是一个例子:
ATTRIBUTE_TYPES = {
id: Field::Number,
name: Field::String,
# ...
employment_types: CheckboxList.with_options(choices: ['Full-time', 'Part-time', 'Freelance', 'Contract-to-hire']),
}.freeze这样,CheckboxList就可以在不同的选项列表中重用。请注意,我没有使用options这个词,因为它已经在Administrate::Field::Base内部使用了,这会产生冲突。
接下来,您的字段还需要模板部分,以告诉Rails如何呈现它。这些文件位于views/文件夹中,示例如下所示:
### app/views/fields/checkbox_list/_index.html.erb
<%= field.data.join(', ') %>### app/views/fields/checkbox_list/_show.html.erb
<%= field.data.join(', ') %>### app/views/fields/checkbox_list/_form.html.erb
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.collection_check_boxes field.attribute, field.choices, :itself, :itself %>
</div>最棘手的是表单。请注意,我使用的是field.choices,它与类CheckboxList中定义的choices方法相同,它从仪表板中给出的选项中读取。
我想就是这样了!将新字段和类型添加到您的仪表板中(不要忘记将其添加到SHOW_PAGE_ATTRIBUTES、FORM_ATTRIBUTES等),您就可以开始工作了。
发布于 2020-01-06 10:21:03
check_box_tag就是你要找的东西。阅读更多here
<div class="form-group">
<label class="font-weight-bold" for="">What type of employment are you looking for?</label>
<% ['Full-time', 'Part-time', 'Freelance', 'Contract-to-hire'].each do |type| %>
<%= check_box_tag "put_your_form_model_here[employment_types][]", type, uniq_number_that_you_want_for_each_type %>
<% end %>
</div>https://stackoverflow.com/questions/59603937
复制相似问题