简单形式 - 编辑时保持单选按钮被选中
Simple form - Keep radio buttons checked when edit
我的单选按钮很少;问题是当我编辑实例时,单选按钮不再被选中(但是当我创建它时它们已经保存在数据库中)。我希望用户以前检查过的单选按钮在编辑时仍然被检查。
_form.html.slim
.cov-pick-row.w-row
- @inspiration_images.each do |img|
- next if img.image.path.nil?
.w-col.w-col-2.w-col-small-4.w-col-tiny-6
label
INPUT[type="checkbox" name="cover[inspiration_image_ids][]" id='cover_inspiration_image_ids' value="#{img.id}" class="hidden"]
.image-cov-pick
= cl_image_tag(img.image.path, height: 190, class: 'img-to-pick', data: { ix: "cover-pick" }).html_safe
设置 checked: true
应该对其进行检查。前任。假设您有 db
列 check_status
存储 checked
值然后使用 checked: img.check_status
.
据我所知,根据您的示例代码,您在 inspiration_image_ids
上节省了 image id
,因此可以使用 inspiration_image_ids.include?(img.id)
使用checked: [#your ids]
示例:
create_table "mymodel", force: :cascade do |t|
t.string "interest_ids", default: [], array: true
end
# _form
<%= simple_form_for(@mymodel) do |f| %>
<%= f.input :interests_ids,
as: :check_boxes, # Display check_boxes
collection: Interest.all, # Search into ids collection
value_method: :id, # Save only the ids
checked: @mymodel.interests_ids, # keep and check the old values
input_html: { multiple: true }
%>
...
我的单选按钮很少;问题是当我编辑实例时,单选按钮不再被选中(但是当我创建它时它们已经保存在数据库中)。我希望用户以前检查过的单选按钮在编辑时仍然被检查。
_form.html.slim
.cov-pick-row.w-row
- @inspiration_images.each do |img|
- next if img.image.path.nil?
.w-col.w-col-2.w-col-small-4.w-col-tiny-6
label
INPUT[type="checkbox" name="cover[inspiration_image_ids][]" id='cover_inspiration_image_ids' value="#{img.id}" class="hidden"]
.image-cov-pick
= cl_image_tag(img.image.path, height: 190, class: 'img-to-pick', data: { ix: "cover-pick" }).html_safe
设置 checked: true
应该对其进行检查。前任。假设您有 db
列 check_status
存储 checked
值然后使用 checked: img.check_status
.
据我所知,根据您的示例代码,您在 inspiration_image_ids
上节省了 image id
,因此可以使用 inspiration_image_ids.include?(img.id)
使用checked: [#your ids]
示例:
create_table "mymodel", force: :cascade do |t|
t.string "interest_ids", default: [], array: true
end
# _form
<%= simple_form_for(@mymodel) do |f| %>
<%= f.input :interests_ids,
as: :check_boxes, # Display check_boxes
collection: Interest.all, # Search into ids collection
value_method: :id, # Save only the ids
checked: @mymodel.interests_ids, # keep and check the old values
input_html: { multiple: true }
%>
...