【问题标题】:How can I have multiple commit buttons for a form in formtastic in Rails 3?在 Rails 3 中,如何在 formtastic 中为表单设置多个提交按钮?
【发布时间】:2011-06-12 19:57:31
【问题描述】:

我有一个表格并且正在使用 Formtastic。我希望按钮有两个选项,并将选择的值存储在新创建的记录中。

我会做这样的事情吗?

<%= form.buttons do %>
  <%= form.commit_button :value => "Give" %>
  <%= form.commit_button :value => "Request" %>
<% end %>

【问题讨论】:

  • 尝试分配一个:name 选项。

标签: ruby-on-rails-3 button formtastic


【解决方案1】:

commit_button() 将第一个参数作为字符串,或者,为了匹配input() API,它还接受:label 选项。这两者在功能上是等价的:

<%= f.commit_button :label => "Save" %>
<%= f.commit_button :label => "Save and Continue Editing" %>

<%= f.commit_button "Save" %>
<%= f.commit_button "Save and Continue Editing" %>

目前还没有用于取消按钮、重置按钮等的 API。您还可以使用标准 Rails 助手,例如 link_to 并在它们周围添加一些标记:

<%= f.buttons do %>
  <%= f.commit_button :label => "Save" %>
  <li class="cancel">
    <%= link_to "Cancel", foos_path %>
  </li>
<% end %>

【讨论】:

  • commit_button 在 2.1.0 中已弃用。我将如何使用最新的格式来做到这一点?
【解决方案2】:

commit_button 已从 api 中删除。我通过在 button_html 中设置按钮的名称和值来实现这一点。

f.actions do
  f.action :submit, :as => :button
  f.action :submit, :as => :button, label: 'Save and Next', button_html: {name: 'boo', value: 'bah'}
  f.action :cancel, :as => :link, label: 'Cancel', class: 'cancel', wrapper_html: {class: :cancel}
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 2011-03-20
    • 1970-01-01
    相关资源
    最近更新 更多