yerassyl
yerassyl

Reputation: 3048

Generating checkboxes in Rails 4

I want to generate some checkboxes depending on select value. So i have select tag:

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

When the value of select changes i want to generate some checkboxes, for that i have div:

<div id="sub_types"> </div>
where i want to generate my checkboxes

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
My method grabs all subtypes and passes them to js file show_sub_types.js.erb

$("#sub_types").html("");
$("#sub_types").append("<%= j render 'show_sub_types', stypes: @stypes %>");
In my js file i render partial show_sub_types.html.erb in which i want to generate my checkboxes:

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

In my partial i do something like this. This code generate me my checkboxes. They look like that:

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

But now i don't know how to submit these checkbox values with my form.I want to store multiple checkbox values in db as array.

Upvotes: 2

Views: 211

Answers (1)

shikha
shikha

Reputation: 669

Please try this

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

Upvotes: 5

Related Questions