【问题标题】:Rails submit button: How do I have button run a certain method when clicked?Rails提交按钮:单击时如何让按钮运行某种方法?
【发布时间】:2012-10-02 21:25:51
【问题描述】:

我正在学习如何构建 Rails 应用程序,但我仍然不完全了解如何让按钮发挥作用。如何使用表单中提供的属性运行特定方法?

【问题讨论】:

    标签: ruby-on-rails-3 forms


    【解决方案1】:
    <%= button_to "Acknowledged", { :controller => 'practice_sessions',
      :id => @practice_session.id}, 
      :method => :put %>
    

    来自https://stackoverflow.com/a/4198918/643500

    阅读http://edgeguides.rubyonrails.org/getting_started.html

    更多示例

    <%= button_to "New", :action => "new" %>
    # => "<form method="post" action="/controller/new" class="button_to">
    #      <div><input value="New" type="submit" /></div>
    #    </form>"
    
    <%= button_to "New", :action => "new", :form_class => "new-thing" %>
    # => "<form method="post" action="/controller/new" class="new-thing">
    #      <div><input value="New" type="submit" /></div>
    #    </form>"
    
    <%= button_to "Create", :action => "create", :remote => true, :form => { "data-type" => "json" } %>
    # => "<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json">
    #      <div><input value="Create" type="submit" /></div>
    #    </form>"
    
    <%= button_to "Delete Image", { :action => "delete", :id => @image.id },
              :confirm => "Are you sure?", :method => :delete %>
    # => "<form method="post" action="/images/delete/1" class="button_to">
    #      <div>
    #        <input type="hidden" name="_method" value="delete" />
    #        <input data-confirm='Are you sure?' value="Delete" type="submit" />
    #      </div>
    #    </form>"
    

    来自http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多