user3797114
user3797114

Reputation:

What's wrong with this input form?

I am a beginner to the ruby/rails language, I only learn by creating things that interest me so I have set out to create a simple bank system.

I haven't got very far, as I'm getting the following error:

ndefined method `text_field' for nil:NilClass

Line 7 of this code:

  <%= form_tag('/deposit') do |f| %>
<div class="field">
  <%= f.text_field :dval %>
</div>
<div class="actions">
  <%= f.submit %>

Here's my bank class

class BankController < ApplicationController
def initialize
    @balance ||= 0 
end


  def deposit
    @dval ||= 0
    @balance = @balance + @dval
  end

  def withdraw
  end
end

I want dval to be the number the user wants to deposit, so if they put 10 and submit the form, it will add dval (10) onto the balance variable.

Any indication as to how I can do this is greatly appreciated.

Upvotes: 0

Views: 53

Answers (1)

usha
usha

Reputation: 29349

your form should be

<%= form_tag('/deposit') do%>
  <div class="field">
    <%= text_field_tag 'dval' %>
  </div>
  <div class="actions">
  <%= submit_tag "Submit"%>
<%end%>

Your controller should be

class BankController < ApplicationController
  def deposit
    @dval = params[:dval].to_i 
    @balance = @balance + @dval
  end
end

Documentation for form_tag

Documentation for text_field_tag

Upvotes: 2

Related Questions