单击html按钮时如何执行rails操作?

时间:2014-10-24 04:15:23

标签: ruby-on-rails ruby html5

我正在尝试在单击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%> &nbsp 
  <%=val%>
<%end%>

2 个答案:

答案 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#cleaninfo_clean_path的定义路线。