5

在 Rails 3 中使用 Twitter Bootstrap 提供的图标作为链接的最佳方式是什么?

目前,我像粘贴的片段一样使用它,但是当我使用平板电脑查看网页时,图标不显示。我确信有更好的方法来使用 Twitter Bootstrap 图标作为 Rails 3 上的链接。

<%= link_to(vote_against_mission_mission_path(:id => mission.id), :method => :post) do %> 

  <i class="icon-chevron-down blank-vote enlarge"></i>

<% end %><br />

<%= link_to(collect_mission_path(controller: "folders", action: "collect", id: mission.id)) do %>

    <i class="icon-heart blank-favorite enlarge" id="actions-centering"></i>
4

5 回答 5

8

如果你构建一个这样的助手:

module BootstrapIconHelper
  def icon_link_to(path, opts = {}, link_opts = {})
    classes = []
    [:icon, :blank].each do |klass|
      if k = opts.delete(klass)
        classes << "#{klass}-#{k}"
      end
    end
    classes << "enlarge" if opts.delete(:enlarge)
    opts[:class] ||= ""
    opts[:class] << " " << classes.join(" ")
    link_to content_tag(:i, "", opts), path, link_opts
  end
end

你可以这样写你的链接:

  <%= icon_link_to(
        vote_against_mission_mission_path(:id => mission.id),
        { :icon => "chevron-down", :blank => "vote", :enlarge => true },
        {:method => :post}
      ) %>
  <%= icon_link_to(
        collect_mission_path(controller: "folders", action: "collect", id: mission.id),
        { :icon => "heart", :blank => "favorite", :enlarge => true, id: "action-centering}
      ) %>
于 2012-05-26T10:05:21.700 回答
7

除非我误解了你所追求的,否则少花点时间:

<%= link_to('', vote_against_mission_mission_path(:id => mission.id), :class => "chevron-down") %> 
于 2012-06-15T18:03:47.173 回答
4

我应该创建这个助手:

module BootstrapHelper
  def icon(*names)
    content_tag(:i, nil, :class => icon_classes(names))
  end

  private
  def icon_classes(*names)
    names.map{ |name| "icon-#{name}" }
  end
end

并像这样使用:

link_to icon(:trash, :white), user_path(@user), method: :delete
于 2013-03-28T22:42:49.153 回答
2

上面的解决方案是返回这个:

<i class="icon-[:remove, :white]"></i>

我改变了一些东西,现在正在为我工​​作:

module BootstrapHelper
  def icon(*names)
    content_tag(:i, nil, :class => icon_classes(names))
  end

  private
  def icon_classes(*names)
    final = ""
    names[0].each do |n|
      final = final + "icon-" + n.to_s + " "
    end
    return final
  end
end

现在它返回它:

<i class="icon-remove icon-white "></i>

用法保持不变:

<%= link_to icon(:remove, :white), doc, :confirm => 'Are you sure?', :method => :delete %>
于 2013-04-10T22:57:25.573 回答
1

使用带有图标引导程序的 link_to

<%= link_to edit_idea_path(idea), class: 'btn btn-default' do %>
    <span class="glyphicon glyphicon-pencil"></span>
    Edit
  <% end %>

<%= link_to new_idea_path, class: 'btn btn-primary btn-lg' do %>
  <span class="glyphicon glyphicon-plus"></span>
  New Idea
<% end %>

  <%= link_to idea, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger' do %>
    <span class="glyphicon glyphicon-remove"></span>
    Destroy
  <% end %>

http://railsgirls.co.il/en/guides/design/list-page/icons.html

于 2015-07-26T21:09:31.537 回答