simple ruby calculator webpage, how to store temporary variables and process them on the same page with submit -
i'm trying create simple page simple calculations , return them on same page. i'll learn use javascript and/or ajax call them without reloading entire page, i'm new programming i'm trying take incremental steps there.
i have been able create form , submit no errors. result returns '0' , i think it's because when page gets submitted , returns same 'index' page clears instance variable. have few question:
- how can variable @result return result (and way of going it?) and
 - is possible load result in vdrop number field?
 - in future want able dynamically add rows (that include each variable, amps, volts, etc.) mean want run calculation more once. assume need use sort of "@variable |f| 1..n" type of formula correct?
 
thanks in advance!
here's controller code:
class calculatorcontroller < applicationcontroller  def calculate    amps = params[:amps].to_i    volts = params[:volts].to_i    distance = params[:distance].to_i    phase = params[:phase].to_i    @result = amps + volts + distance + phase    render 'index' end  end   here's index.html.erb code calculator page:
<div class="calculator index">     <h2>calculator test</h2> <div>     <table class="table table-condensed">         <tr>             <th>amps</th>             <th>volts</th>             <th>phase</th>             <th>distance</th>             <th>vdrop</th>         </tr>    <%= form_tag({:controller => "calculator", :action => "calculate"}, :method => "post") %>   <tr>     <td><%= number_field 'amps' , :class => 'text_field', :step => 'any' %></td>     <td><%= number_field 'volts', :class => 'text_field', :step => 'any' %></td>     <td><%= number_field 'phase', :class => 'text_field', :step => 'any' %></td>     <td><%= number_field 'distance', :class => 'text_field', :step => 'any' %></td>       <td><%= number_field 'vdrop', :class => 'text_field', :step => 'any' %></td>     </tr>       </table>      <%= submit_tag('submit')%> </div> <div>      <%= @result %> </div>  <% end %> </div>      
the problem here you're trying pass instance variables, instances of action other actions cause technically index.html.erb template being rendered due index action don't have. i'd is:
class calculatorcontroller < applicationcontroller  def index    if params[:amps]      amps = params[:amps].to_i      volts = params[:volts].to_i      distance = params[:distance].to_i      phase = params[:phase].to_i      @result = amps + volts + distance + phase    else      @result = "make calculation"    end end  end   your form:
<%= form_tag ({controller: 'calculators', action: 'index'}), method: :get %>      <td><%= number_field_tag :amps , nil %></td>     <td><%= number_field_tag :volts, nil %></td>     <td><%= number_field_tag :phase, nil %></td>     <td><%= number_field_tag :distance, nil %></td>       <td><%= number_field_tag :vdrop, nil %></td>     <%= submit_tag('submit')%> <% end %>     <%= @result %>   will work. tried it.
number of things make work: 1. it's number_field_tag not number_field. 2. try not use "amps" params instead use :amps, if params[:amps] wasn't detecting "amps" unless did params["amps"] 
controller same.
Comments
Post a Comment