Ruby-on-rails 将 link_to 与嵌入的 HTML 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9401942/
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
Using link_to with embedded HTML
提问by Vanessa L'olzorz
I'm using Twitter's Bootstrap stuff and I have the following HTML:
我正在使用 Twitter 的 Bootstrap 东西,我有以下 HTML:
<a class="btn" href="<%= user_path(@user) %>"><i class="icon-ok icon-white"></i> Do it@</a>
What's the best way to do this in Rails? I'd like to use <%= link_to 'Do it', user_path(@user) %>but the <i class="icon-ok icon-white"></i>is throwing me off?
在 Rails 中执行此操作的最佳方法是什么?我想使用<%= link_to 'Do it', user_path(@user) %>但<i class="icon-ok icon-white"></i>它让我失望?
回答by Veraticus
Two ways. Either:
两种方式。任何一个:
<%= link_to user_path(@user) do %>
<i class="icon-ok icon-white"></i> Do it@
<% end %>
Or:
或者:
<%= link_to '<i class="icon-ok icon-white"></i> Do it@'.html_safe, user_path(@user) %>
回答by Eric Farkas
I had the same need recently. Try this:
我最近也有同样的需求。尝试这个:
<%= link_to '<i class="icon-ok icon-white"></i> Do it'.html_safe, user_path(@user) %>
<%= link_to '<i class="icon-ok icon-white"></i> Do it'.html_safe, user_path(@user) %>
回答by Renshuki
You have also the possibility to create an helper method like below:
您还可以创建如下所示的辅助方法:
def link_fa_to(icon_name, text, link)
link_to content_tag(:i, text, :class => "fa fa-#{icon_name}"), link
end
Adapt the classes to your needs.
根据您的需要调整课程。
回答by Justin Herrick
If you want a link in rails that uses that same icon class from twitter bootstrap all you need to do is something like this.
如果您想要在 rails 中使用来自 twitter bootstrap 的相同图标类的链接,您需要做的就是这样。
<%= link_to "Do it@", user_path(@user), :class => "btn icon-ok icon-white" %>
回答by ddavison
Using HAML:
使用 HAML:
= link_to model_path do
%img{src: '/assets/someimg.png'}
回答by eveevans
In the gem twitter-bootstrap-rail : they create a helper glyph
在 gem twitter-bootstrap-rail 中:他们创建了一个辅助字形
def glyph(*names)
content_tag :i, nil, :class => names.map{|name| "icon-#{name.to_s.gsub('_','-')}" }
end
So you can use it like: glyph(:twitter)and you link helper could look like: link_to glyph(:twitter), user_path(@user)
所以你可以像这样使用它:glyph(:twitter)并且你的链接助手看起来像:link_to glyph(:twitter), user_path(@user)
回答by Ovi
In normal HTML we do,
在普通的 HTML 中,我们这样做,
<a href="register.html"><i class="fa fa-user-plus"></i> Register</a>
In Ruby On Rails:
在 Ruby On Rails 中:
<%= link_to routeName_path do %>
<i class="fa fa-user-plus"></i> Link Name
<% end %>
<%= link_to register_path do %>
<i class="fa fa-user-plus"></i> Register
<% end %>
回答by Webdevotion
I will give this a shot since you haven't accepted an answer yet
and the other answers are not 100% what you were looking for.
This is the way to do it the Rails way.
我会试一试,因为您还没有接受答案
,而其他答案不是您想要的 100%。
这是使用 Rails 的方法。
<%= link_to(user_path(@user), :class => 'btn') do %>
<i class="icon-ok icon-white"> </i> Do it!
<% end %>
Edit: leaving my answer for future reference,
but @justin-herrick has the correct answer when
working with Twitter Bootstrap.
编辑:留下我的答案以供将来参考,
但@justin-herrick 在
使用 Twitter Bootstrap时有正确的答案。
回答by Titas Milan
I think you can simplified it through a helper method if you use it frequently in your application.
如果您在应用程序中经常使用它,我认为您可以通过辅助方法来简化它。
put it in helper/application_helper.rb
把它放在 helper/application_helper.rb
def show_link(link_text, link_source)
link_to("#{content_tag :i, nil, class: 'icon-ok icon-white'} #{link_text}".html_safe,
link_source, class: "btn")
end
Then call it from your view file just like link_to
然后从您的视图文件中调用它,就像 link_to
<%= show_link "Do it", user_path(@user) %>
回答by Kleber S.
If you are using the bootstrap 3.2.0, you can use this helper in your app/helpers/application_helper.rb
如果你使用的是 bootstrap 3.2.0,你可以在你的 app/helpers/application_helper.rb
module ApplicationHelper
def glyph(*names)
content_tag :i, nil, :class => names.map{|name| "glyphicon glyphicon-#{name.to_s.gsub('_','-')}" }
end
end
and then, in your views:
然后,在您看来:
link_to glyph(:pencil) + ' Edit', edit_post_path(@post), class: 'btn btn-warning'


