简单的ruby计算器网页,如何存储临时变量并使用submit在同一页面上处理它们
我正在尝试创建一个简单的页面,它将执行简单的计算并在同一页面上返回它们。最终,我将学习使用javascript和/或ajax调用它们,而无需重新加载整个页面,但我对编程还不熟悉,因此我尝试采取增量步骤来实现这一点 我已经能够创建表单并提交,没有错误。结果返回“0”,我认为这是因为当页面被提交并返回相同的“索引”页面时,它会清除实例变量。我有几个问题:简单的ruby计算器网页,如何存储临时变量并使用submit在同一页面上处理它们,ruby,variables,ruby-on-rails-4,Ruby,Variables,Ruby On Rails 4,我正在尝试创建一个简单的页面,它将执行简单的计算并在同一页面上返回它们。最终,我将学习使用javascript和/或ajax调用它们,而无需重新加载整个页面,但我对编程还不熟悉,因此我尝试采取增量步骤来实现这一点 我已经能够创建表单并提交,没有错误。结果返回“0”,我认为这是因为当页面被提交并返回相同的“索引”页面时,它会清除实例变量。我有几个问题: 我如何才能让变量@result实际返回结果(这是一种很好的方法吗?) 是否可以在Vdrop编号字段中加载结果 在未来,我希望能够动态添加行(包括每
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
类计算器控制器
以下是计算器页面的index.html.erb代码:
<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") do %>
<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>
计算机测试
安培
伏特
阶段
距离
VDrop
“计算器”,:操作=>“计算”},:方法=>“发布”)do%>
'文本\字段',:步骤=>'任何'%>
'文本\字段',:步骤=>'任何'%>
'文本\字段',:步骤=>'任何'%>
'文本\字段',:步骤=>'任何'%>
'文本\字段',:步骤=>'任何'%>
这里的问题是您试图将实例变量(该操作的实例)传递给其他操作,因为从技术上讲,index.html.erb模板是由于您没有索引操作而呈现的。我要做的是:
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 a calculation"
end
end
end
类计算器控制器
您的表格:
<%= form_tag ({controller: 'calculators', action: 'index'}), method: :get do %>
<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 %>
会有用的。我试过了
让它工作的事情有很多:1。这是数字字段标记,而不是数字字段。2.尽量不要对参数使用
“AMPS”
,而是使用:AMPS
,如果参数[:AMPS]没有检测到“AMPS”
,除非我检测到参数[“AMPS”]
控制器是相同的 你所说的“实际返回结果”是什么意思?我试图返回@return instance varaible result,即:“@result=amps+volts+distance+phase”我用提供的代码替换了代码,并得到了一个:“Showing C:/Users/Kevin/Desktop/sites/solar_permit/app/views/calculator/index.html.erb,其中第14行出现了:“错误。关于类控制器的一个问题-使用“if params[:amps]”部分,您是否只是检查该字段中是否有实际数据?另外,我没有“calculate.html.erb”页面,只有index.html.erb页面……if params[:amps]正在检查params中是否有与此相关的内容。我不知道第14行在这里。请确保将操作名称更改为index not Calculator。我收到此错误:找不到Calculator控制器的操作“show”。我在转到索引时也收到此错误:未定义的局部变量或方法“calculators\u path”#