Rails提交按钮:如何在单击时按钮运行某个方法?

时间:2012-10-02 21:25:52

标签: ruby-on-rails-3 forms

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

1 个答案:

答案 0 :(得分:8)

<%= 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