Yogesh
Yogesh

Reputation: 37

how to save multiple checkbox values in rails?

I'm doing fixeddeposit project, in opening account page (i.e.,) in _form. I'm having checkbox to select the deposit amount using javascript. Where deposit amount's datatype is float. It add the value on that page only, when i am trying to hit the submit button it shows the folowing error message

undefined method `to_f' for ["0", "0", "0", "0", "0", "0", "0", "0", "0", "100000", "0", "0"]:Array

** my _form.html.erb**

<tr>
  <th><%= f.label :click_your_deposit_amount %></th>    
</tr>
<tr>
  <td> 
    <%= f.check_box :depamt, {:multiple => "true"}, 500 %>500

    <%= f.check_box :depamt, {:multiple => "true"}, 1000 %>1000

    <%= f.check_box :depamt, {:multiple => "true"}, 2000 %>2000

    <%= f.check_box :depamt, {:multiple => "true"}, 3000 %>3000

    <%= f.check_box :depamt, {:multiple => "true"}, 4000 %>4000

    <%= f.check_box :depamt, {:multiple => "true"}, 5000 %>5000

    <%= f.check_box :depamt, {:multiple => "true"}, 10000 %>10000

    <%= f.check_box :depamt, {:multiple => "true"}, 50000 %>50000

    <%= f.check_box :depamt, {:multiple => "true"}, 100000 %>100000

    <%= f.check_box :depamt, {:multiple => "true"}, 500000 %>500000

    <%= f.check_box :depamt, {:multiple => "true"}, 1000000 %>1000000
  </td>
</tr>    
<tr>
  <td>
    <span id="span"></span>
    <div>Total Deposit Amount: <span id="amt"> </span></div>
  </td>
</tr>

<script>
  $('input:checkbox').change(function(){ 
    var total = 0;
    $('input:checkbox:checked').each(function(){
      total+=parseInt($(this).val());
      $('#amt').html(total)
      }); 
    });
</script>

here is my controller

fds_controller.rb

 class FdsController < ApplicationController

  def new
    @fd = Fd.new
  end

  def create

    @fd = Fd.new(params[:fd])
    if @fd.save
      flash[:success] = "FD Account Opened Successfully!"
      redirect_to fds_path
    else
      flash[:error] = "Couldn't Open FD"  
      render  'new'
    end

  end

end

I dono how to store the checked values in my database(sqlite3).

Kindly help me to solve this issue.

-Thanks... :)

Upvotes: 0

Views: 2051

Answers (2)

Wizard of Ogz
Wizard of Ogz

Reputation: 12643

Probably the easiest thing to do is create a "depamt" serialized column for Fds (typically a text column is used in the database). ActiveRecord will automatically persist and fetch Array values for you. See the documentation here for examples of configuring your model.

Upvotes: 0

Alex Lynham
Alex Lynham

Reputation: 1318

The problem is that you are trying to call the .to_f method on an Array. You could call that method on items in the array, but not the array itself:

new_array = my_array.map(&:to_f)

This should convert all the items to floats and set this as a new array.

Upvotes: 1

Related Questions