简体   繁体   English

使用link_to替代Rails中的anchor标签是否有优势?

[英]Is there an advantage to using link_to over the anchor tag in Rails?

Don't these two do the same thing? 这两个人不是在做同一件事吗?

<%= link_to "Example", '#', class: "somestyle" %>

<a href="#" class= "somestyle"> Example </a>

If I'm writing a static .html.erb page, if everything else is written with HTML tags, doesn't it make sense to use HTML tags for links as well? 如果我正在编写静态.html.erb页面,而其他所有内容都是使用HTML标签编写的,那么也将HTML标签用于链接是否有意义? I'm not sure why one should use a helper. 我不确定为什么要使用一个助手。 Similarly, for linking style sheets, javascripts, etc. 同样,用于链接样式表,javascript等。

For the link tags, it may not make a difference which way you go. 对于链接标记,走哪条路可能没有什么不同。 Unless you're linking to more than "#". 除非您链接的链接不止是“#”。 For instance, using a routed path. 例如,使用路由路径。

For the stylesheets and javascript, I think you will need to continue to use the Rails helpers if you're taking advantage of the asset pipeline. 对于样式表和javascript,我认为如果您要利用资产管道,则需要继续使用Rails帮助器。 If so, the hash in the filename changes at each asset compilation (I believe), and manually trying to edit the filename each time could become a pain. 如果是这样,文件名中的哈希值会在每次资产编译时更改(我相信),并且每次尝试手动编辑文件名都可能会很麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM