Ruby-on-rails 带有 simple_form 的按钮内的 HTML 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10858582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:29:01  来源:igfitidea点击:

HTML code inside buttons with simple_form

ruby-on-railsrubytwitter-bootstrapsimple-form

提问by Bruno Campos

I'm new to rails, and just found the simple_form gem. I installed it with bootstrap suport, but now I can't get this code to work the way I want it

我是 Rails 的新手,刚刚找到了 simple_form gem。我用引导程序支持安装了它,但现在我无法让这段代码按照我想要的方式工作

<%= f.button :submit, "<i class='icon-ok icon-white'></i> Save", class: "btn btn-primary" %>

I just want to put the icon inside the button, but when I do this, it shows me a button with the text '<i class='icon-ok icon-white'></i> Save'

我只想将图标放在按钮内,但是当我这样做时,它会向我显示一个带有文本“<i class='icon-ok icon-white'></i> Save”的按钮

I also tried to do

我也尝试做

<%= f.button :submit, class: "btn btn-primary" do %><i class="icon-ok icon-white"></i> Save<% end %>

But with no success. How can I add some HTML inside the button with simple_form gem?

但没有成功。如何使用 simple_form gem 在按钮内添加一些 HTML?

回答by Undistraction

Don't use content_tag. The following works:

不要使用 content_tag。以下工作:

  <%= button_tag(type: 'submit', class: "btn btn-primary") do %>
    <i class="icon-ok icon-white"></i> Save
  <% end %>

回答by Bombazook

In simple_form 3.0rc use :button button type (it passes your block to original ActiveView button helper):

在 simple_form 3.0rc 中使用 :button 按钮类型(它将您的块传递给原始 ActiveView 按钮助手):

<%= f.button :button do %>
  <i class="icon-save"></i>
  Commit
<% end %>

Or write additional button wrapper.

或者编写额外的按钮包装器。

For additional info look into simple_form/form_builder.rb FormBuilder#button method.

有关其他信息,请查看 simple_form/form_builder.rb FormBuilder#button 方法。

回答by Ismael Abreu

I think you can't do it with simple_form. But I have good news for you. You should be fine using rails helper along side with simple form.

我认为你不能用 simple_form 做到这一点。但我有个好消息要告诉你。您应该可以将 rails helper 与简单的形式一起使用。

just do

做就是了

button_tag(type: 'submit', class: "btn btn-primary") do
  content_tag(:i, class: "icon-ok icon-white")
  "Save"
end

Not sure if this works, even the syntax but it should give you a hint

不确定这是否有效,即使是语法,但它应该给你一个提示

回答by jasonleonhard

One line example submit button in Rails with bootstrap btn class:

Rails 中带有 bootstrap btn 类的一行示例提交按钮:

<%= button_tag(type: 'submit', class: "btn btn-primary") do %> Save <% end %>

回答by stephenmurdoch

You can do this with the following code:

您可以使用以下代码执行此操作:

= f.button :button, 'Send', data: { disable_with: "<i class='fi-heart'></i> Sending..." }

Note that you want to use f.buttoninstead of f.submitAlso note that :buttonneeds to be the first param to f.button

请注意,您要使用f.button而不是f.submit另请注意,:button需要是第一个参数f.button