在 Rails 4 中生成复选框

yer*_*syl 2 html javascript checkbox ruby-on-rails

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

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

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

<div id="sub_types"> </div>
Run Code Online (Sandbox Code Playgroud)
我想在哪里生成我的复选框

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
Run Code Online (Sandbox Code Playgroud)
我的方法抓取所有子类型并将它们传递给 js 文件 show_sub_types.js.erb

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

<% stypes.each do |type| %>
	<%= check_box_tag "subtype", type.id %>
	<%= type.subTypeName %>
	<br>
<% end %>
Run Code Online (Sandbox Code Playgroud)

在我的部分我做这样的事情。此代码生成我的复选框。他们看起来像这样:

<input type="checkbox" name="subtype" id="subtype" value="1">
Run Code Online (Sandbox Code Playgroud)

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

dja*_*dam 5

请试试这个

<% stypes.each do |type| %>
  <%= check_box_tag 'stype_ids[]', type.id %>
  <%= type.subTypeName %>
  <br>
<% end %>
Run Code Online (Sandbox Code Playgroud)