ruby-on-rails 在Rails button_to中嵌入font-awesome图标

mrwjdhj3  于 4个月前  发布在  Ruby
关注(0)|答案(5)|浏览(79)

我想要一个按钮如下:

[ Sign in with FB]

字符串
其中FB是一个字体很棒的图标。我尝试了下面的方法,但不知道如何将图标嵌入到按钮中:

= button_to "Login with", user_omniauth_authorize_path(:facebook)


下面是font-awesome的正常调用方式(在haml中):

%i.icon-facebook-sign


我如何达到我想要的效果?
谢谢

bzzcjhmw

bzzcjhmw1#

你可以像这样把一个块传递给button_to

= button_to user_omniauth_authorize_path(:facebook) do
    Login with
    %i.icon-facebook-sign

字符串
(虽然我不知道为什么你不直接使用Facebook图标本身作为按钮。

clj7thdc

clj7thdc2#

为button_to助手添加类选项

= button_to "Login with", user_omniauth_authorize_path(:facebook), :class => 'icon-facebook-sign'

字符串

j91ykkif

j91ykkif3#

试试这个代码,它为我运行

<%= button_to line_items_path(product_id: produit.id),class:"btn btn-outline-primary" do %>
    <i class="fas fa-shopping-basket"></i>
<% end %>

字符串
只要改变图标

rlcwz9us

rlcwz9us4#

您可以创建一个使用button标记但自定义输出的helper方法:

#application_helper.rb

def button_to_with_icon(text, path, classes)
  form_tag path, :method => :post do
    button_tag(classes) do
      raw text
    end
  end
end

字符串
然后调用helper方法,并将原始html作为参数嵌入:

<%= button_to_with_icon("login with <i class='fa fa-facebook-official'></i>", { action: "omniauth_authorize", controller: "users" }, class: "btn btn-info") %>


动作、控制器和类设置只是示例。但我认为您可以修改此设置以满足您的需要。

2q5ifsrm

2q5ifsrm5#

这是我做的对我有用的助手:

def html_button_to(html = nil, options = nil, html_options = nil)
  button_to(options, html_options) do
    html
  end
end

字符串
结合font-awesome-rails gem,它可以让你做:

html_button_to fa_icon(:facebook, text: "Login with"), user_omniauth_authorize_path(:facebook)

相关问题