SEO/Rails - 如何向每个“link_to”添加标题标签

发布于 2024-09-24 13:14:39 字数 357 浏览 3 评论 0原文

我很惊讶 Rails 的创建者没有想到这一点,如果有人能提供帮助,那就太好了。

我们怎样才能

<%= link_to "My Title", :controller => "products" %>

自动改变这一点:

<%= link_to "My Title", :controller => "products", :title => "My Title" #basically a copy of the text %>

我认为这对SEO有很大帮助。

多谢!

亚历克斯

I'm surprised Rails creator didn't think about that, if someone can help, would be great.

How can we do to change this:

<%= link_to "My Title", :controller => "products" %>

to this automatically:

<%= link_to "My Title", :controller => "products", :title => "My Title" #basically a copy of the text %>

I think it could help SEO a lot.

Thanks a lot!

Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山人契 2024-10-01 13:14:39

这是 Rails 3 方式:

<%= link_to object_path, title: "Path Title" %>

进一步阅读:https://www.searchenginejournal.com/how-to-use-link-title-attribute- Correctly/

This is the rails 3 way:

<%= link_to object_path, title: "Path Title" %>

Further reading: https://www.searchenginejournal.com/how-to-use-link-title-attribute-correctly/

最美不过初阳 2024-10-01 13:14:39

你的问题是有效的,我不知道你为什么被否决,但是,rails 的创建者确实考虑过这一点。实际上,您可以以非常简单的方式完成此操作,而不是使用自定义方法使事情变得复杂:

<%= link_to "Link", { :action => "show" }, { :title => "Title" } %>

您实际上可以添加任何您喜欢的参数,而不仅仅是标题。

希望这有帮助!

Your question is valid and I don't know why you are down-voted, but, the creator of rails DID actually think about this. Actually, you can do it in a very simple manner instead of complicating using a custom method:

<%= link_to "Link", { :action => "show" }, { :title => "Title" } %>

You can in fact add any parameter you like, not just the title.

Hope this helps!

御守 2024-10-01 13:14:39

尝试类似的方法

def link_to_with_autotitle(title, args = {})
  link_to_without_autotitle(title, args.merge(:title => title))
end
alias_method_chain :link_to, :autotitle

还没有测试代码,也不记得确切的 link_to 规范,但我认为你明白了

Try something like that

def link_to_with_autotitle(title, args = {})
  link_to_without_autotitle(title, args.merge(:title => title))
end
alias_method_chain :link_to, :autotitle

Haven't tested the code and do not remember the exact link_to spec but I think you get the idea

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文