0

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... :)

10
  • Could you please print params[:fd] in the method and post the log here? Commented May 30, 2014 at 12:08
  • @visnu: you mean heroku logs?. Sorry, i'm new to programming and ruby on rails. Commented May 30, 2014 at 12:14
  • No. Are you running the rails app locally(YOur Machine)? Commented May 30, 2014 at 12:15
  • yes. but, i dono how to see the local log... :( Commented May 30, 2014 at 12:16
  • in rails server it shows: Completed 500 Internal Server Error in 1.7ms NoMethodError (undefined method to_f' for ["0", "0", "0", "0", "0", "0", "0", "0", "0", "100000", "0", "0"]:Array): app/controllers/fds_controller.rb:7:in new' app/controllers/fds_controller.rb:7:in `create' Commented May 30, 2014 at 12:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.