【问题标题】:How to get submit button value in form builder?如何在表单构建器中获取提交按钮值?
【发布时间】:2013-01-31 23:03:51
【问题描述】:

我正在使用 Rails 3.2 上的 simple_form gem 创建一个表单。

我遇到的问题是我希望在表单的f.button :submit 行中添加一个font-awesome 图标。由于输出类型为input,我无法在其中嵌入图标元素。

我可以简单地将 submit 按钮硬编码到包含图标的表单中,但我不确定如何获取按钮值 - Create %model/Update %model

我的代码贴在下面:

<div id="form">
  <%= simple_form_for @program, :html => { :class => 'form-horizontal' } do |f| %>
    <%= f.input :name %>
    <%= f.input :url %>
    <%= f.input :description, :input_html => { :class => "span5", :rows => 10 } %>

    *********************

    <%= f.button :submit do %>
      <i class="icon-save"></i> What Here??? <--- Doesn't work since output is type input
    <% end %>

    *********************

    <button class="btn">
      <i class="icon-save"></i> <%= f.button(:submit).label %> <--- Doesn't work, alternative?
    </button>
  <% end %> 
</div>

预期输出:

<button type="submit" class="btn">
    <i class="icon-plus-sign"></i> Create *ModelName*        
</button>

或更新 ModelName,具体取决于控制器操作。

【问题讨论】:

  • 你能告诉我们它在 html 代码中的样子吗?

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 simple-form font-awesome


【解决方案1】:
<%= f.button do %>
    <i class="icon-save"></i> <%= "#{f.object.new_record? ? 'Create' : 'Update'} #{f.object.class.model_name.human}" %>
<% end %>

最好为此制作一些辅助方法。如果您想使用 I18n,请查看 source codebuttonsubmit_default_value 方法,17631776 行)。很清楚。

【讨论】:

    猜你喜欢
    • 2010-10-26
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    相关资源
    最近更新 更多