【问题标题】:How to execute a rails action when html button is clicked?单击html按钮时如何执行rails动作?
【发布时间】:2014-10-24 04:15:22
【问题描述】:

我正在尝试在单击 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%>

【问题讨论】:

标签: ruby-on-rails ruby html


【解决方案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"

【讨论】:

  • 你能解释一下:class =&gt; 'btn btn-primary'这个部分吗
  • @ErielMarimon 这是一个button,它将锚链接作为按钮。仅当您使用引导程序时,该样式才适用
【解决方案2】:

我的 Rails 有点生锈了,但我认为 &lt;%= button_to "clean", info_clean_path %&gt; 之类的东西可以工作,前提是 InfoController#clean 具有 info_clean_path 的定义路由。

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多