Html Rails 和 <span> 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2685436/
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-08-29 02:47:19  来源:igfitidea点击:

Rails and the <span> tag

ruby-on-railshtml

提问by Zachary Wright

I'm fairly new to Ruby on Rails, and I'm attempting to create some fancy CSS buttons using the "sliding doors" technique. I have it almost working, but I feel like there has to be a better way to handle the tags for a link.

我是 Ruby on Rails 的新手,我正在尝试使用“滑动门”技术创建一些漂亮的 CSS 按钮。我几乎可以正常工作,但我觉得必须有更好的方法来处理链接的标签。

The way I'm currently doing it:

我目前的做法是:

 <%= link_to '<span>New car</span>', {:action => "new"}, :class=>"button" %> 

This isn't terrible, per se, but I would like to know if this is the best way to handle span tags in RoR.

这本身并不可怕,但我想知道这是否是在 RoR 中处理跨度标签的最佳方式。

回答by ryeguy

Another option is this:

另一种选择是这样的:

<%= link_to content_tag(:span, 'New car'), {:action => "new"}, :class=>"button" %>

docs

文档

回答by Jimmy Baker

Or you could be pro and use named routes/resources + Haml. That would make it look like:

或者您可以成为专业人士并使用命名路线/资源 + Haml。这将使它看起来像:

%a{ :href => new_car_path }
  %span New Car

What you have is fine though..

不过你的东西很好..

回答by alex.zherdev

If you're still curious, here are some ways to rewrite your code:

如果您仍然好奇,这里有一些重写代码的方法:

Use content_tag:

使用content_tag

<%= link_to content_tag("span", "New car"), {:action => "new"}, :class=>"button" %>

Use link_towith a block:

使用link_to带块:

<%= link_to {:action => "new"}, :class=>"button" do %>
  <span>New card</span>
<% end %>

And of course, you can combine the two by putting a content_taginside the block, but I'll leave it to the reader as an exercise :)

当然,您可以通过content_tag在块内放置 a 来将两者结合起来,但我将把它留给读者作为练习:)