我正在尝试在单击html按钮时清理数组。我有这个控制器:
class InfoController < ApplicationController
$names = {}
def show
render :print
end
def as
@name = params[:myinfo][:name]
@number = params[:myinfo][:number]
$names[@name] = @number
render :test
end
def clean
$names = {}
end
def add
end
end
如果单击此表单中的按钮,我将如何执行clean
操作?
<h1>Info print</h1>
<%= form_for :myinfo do |a| %>
<%= a.label :name %>
<%= a.text_field :name %><br>
<%= a.label :number%>
<%= a.text_field :number%><br>
<%= a.submit %>
<%end%>
<form>
<input type="button" onClick="">
</form>
<%= $names.each do |key, val|%>
<%=key%>  
<%=val%>
<%end%>
答案 0 :(得分:1)
您可以这样写:
<form>
<%= link_to "clean", clean_path, :class => 'btn btn-primary'%> # you can add btn class if you are using bootstarp
</form>
<强> routes.rb中:强>
get 'clean' => "info#clean"
答案 1 :(得分:0)
我的Rails有点生疏,但我认为<%= button_to "clean", info_clean_path %>
之类的内容会有效,只要InfoController#clean
有info_clean_path
的定义路线。