Q:

How to select only checked records using check_box_tag?

Guys my check_box_tag looks like as follows
<%= form_tag({:action => 'update_survey_list_status',:projectid=>params[:id], :status=>4}, :id => 'to_be_approved_frm') do %>
  <% @publishedlist.each do |b| %>
    <%= fields_for "beneficiaryloan[#{b.id}]" do |bloan| %>
      <%= bloan.text_field :amount, :class=>'forms_txtbx'%>
      <%= bloan.text_field :rate, :class=>'forms_txtbx'%>
      <%= bloan.text_field :period, :class=>'forms_txtbx'%>
    <% end %> 
    <%= check_box_tag "benificiary_id[#{b.id}]",b.id,:name => "benificiary_id[]"%>
  <% end %>
  <%= submit_tag "Approve", :class=>'form_buttons'  %>
<% end %>

And in controller, I'm reading all the beneficiary ids like this
params[:beneficiaryloan].each do |key, value|
      beneficiary = Beneficiary.find(key) rescue nil
      @benefciary_loan=beneficiary.beneficiaryloans.build(value)
      @benefciary_loan.beneficiary_id=beneficiary.id
      @benefciary_loan.hfi_id=session[:id].to_s
      @benefciary_loan.status_id=params[:status]
      @benefciary_loan.save if beneficiary
end

What I need is, Inserting all the beneficiary ids to [beneficiaryloans] table which are checked, but in my case it inserting all records even some of them are unchecked.
How to do I select only checked ids?

A:

Try changing your check_box_tag to
<%= check_box_tag "beneficiaryloan[#{b.id}][enabled]", 1, true %>

Then in your controller do the following:
params[:beneficiaryloan].select{|k,v| v.delete(:enabled).to_i > 0 }.each do |k,v|
  ..
end

Since the enabled attribute has no influence in the model you can just delete it out of the resulting beneficiary_load hashes.

