在Rails 4中生成复选框

Generating checkboxes in Rails 4

本文关键字:复选框 Rails      更新时间:2023-09-26

我想根据选择值生成一些复选框。所以我选择了标签:

<%= f.collection_select :type, RequestType.order(:typeName), :id, :typeName,
 {include_blank:true }, {:class => "types"} %>

当select的值改变时,我想生成一些复选框,为此我有div:

<div id="sub_types"> </div>
我想在哪里生成我的复选框

def show_sub_types
	@rtype = params[:id];
        @stypes = RequestSubType.where("request_type_id=?", @rtype).all
	 respond_to do |format|
  	 format.js
	 format.html
	end
end
我的方法获取所有子类型并将它们传递到js文件show_sub_types.js.erb

$("#sub_types").html("");
$("#sub_types").append("<%= j render 'show_sub_types', stypes: @stypes %>");
在我的js文件中,我呈现了部分show_sub_types.html.erb,我想在其中生成复选框:

<% stypes.each do |type| %>
	<%= check_box_tag "subtype", type.id %>
	<%= type.subTypeName %>
	<br>
<% end %>

在我的部分,我做了这样的事情。这段代码生成了我的复选框。它们看起来是这样的:

<input type="checkbox" name="subtype" id="subtype" value="1">

但现在我不知道如何在表单中提交这些复选框值。我想将多个复选框值作为数组存储在数据库中。

请尝试此

<% stypes.each do |type| %>
  <%= check_box_tag 'stype_ids[]', type.id %>
  <%= type.subTypeName %>
  <br>
<% end %>