문제

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

도움이 되었습니까?

해결책

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top