Question

I am trying to create a simple calculator application .

calculator.html.erb

<input type=text name="operand_one" value="0"></td>
<input type=text name="operand_two" value="0"></td>
.
.
.
<input type=text name="output" value="<% =@result %>"></td>

I am able to get the output correctly but the values of the first two text boxes changes to 0 , I need to know how to retain these values ..

Was it helpful?

Solution

You're hardcoding the value to "0" with this part of the code

value="0"

you could change the lines to

<input type=text name="operand_one" value="@value_one"></td> #etc...

and in your controller

@value_one = 0 unless X

but X depends on how your controller works - do you go to a new action to solve the problem in the calculator? if so you'll have to use the params hash.

Not related to the question, but why are you using text for your calculation fields? and are you hard coding that html or it's just the output?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top