在 Rails 中定义锚标记的正确方法是什么?

发布于 2024-08-18 07:32:55 字数 494 浏览 6 评论 0原文

文档(和谷歌)中可以明显看出如何生成与片段链接,例如 podcast/5#comments。您只需将 :anchor 的值传递给 link_to 即可。

我关心的是生成 Comments 标记(即第一个链接的目的地)的更简单的任务。

我尝试了以下方法,虽然它们似乎有效,但标记不是我所期望的:

link_to "Comments", :name => "comments"
link_to "Comments", :anchor => "comments"

我认为我错过了一些明显的东西。谢谢。

It's obvious from the documentation (and google) how to generate a link with a segment e.g. podcast/5#comments. You just pass a value for :anchor to link_to.

My concern is about the much simpler task of generating the <a name="comments">Comments</a> tag i.e. the destination of the first link.

I've tried the following, and although they seemed to work, the markup was not what I expected:

link_to "Comments", :name => "comments"
link_to "Comments", :anchor => "comments"

I think I'm missing something obvious. Thanks.

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

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

发布评论

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

评论(3

战皆罪 2024-08-25 07:32:55

您对 Ruby 的语法糖(Rails 大量使用)感到困惑。在回答你的问题之前,让我先简单解释一下。

当 ruby​​ 函数采用单个散列参数时:

def foo(options)
  #options is a hash with parameters inside
end

您可以“忘记”放置圆括号/方括号,并像这样调用它:

foo :param => value, :param2 => value

Ruby 会填写空白并理解您要完成的任务是:

foo({:param => value, :param2 => value})

现在,对于您的问题:link_to 采用两个可选哈希 - 一个称为options,另一个称为html_options。你可以想象它是这样定义的(这是一个近似值,它要复杂得多)

def link_to(name, options, html_options)
...
end

现在,如果你这样调用它:

link_to 'Comments', :name => 'Comments'

Ruby 会有点困惑。它会尝试为您“填写空白”,但这是错误的:

link_to('Comments', {:name => 'Comments'}, {}) # incorrect

它会认为 name => 'Comments' 部分属于选项,而不是 html_options

你必须自己填补空白来帮助 ruby​​。将所有括号放在适当的位置,它将按预期运行:

link_to('Comments', {}, {:name => 'Comments'}) # correct

如果需要,您实际上可以删除最后一组括号:

link_to("Comments", {}, :name => "comments") # also correct

不过,为了使用 html_options,您必须保留第一组括号。例如,您需要对带有确认消息和名称的链接执行此操作:

link_to("Comments", {:confirm => 'Sure?'}, :name => "comments")

其他 Rails 助手具有类似的结构(即 form_forcollection_select),因此您应该了解这一点技术。如有疑问,只需添加所有括号即可。

You are getting confused by Ruby's syntactic sugar (which Rails uses profusely). Let me explain this briefly before answering your question.

When a ruby function takes a single parameter that is a hash:

def foo(options)
  #options is a hash with parameters inside
end

You can 'forget' to put the parenthesis/brackets, and call it like this:

foo :param => value, :param2 => value

Ruby will fill out the blanks and understand that what you are trying to accomplish is this:

foo({:param => value, :param2 => value})

Now, to your question: link_to takes two optional hashes - one is called options and the other html_options. You can imagine it defined like this (this is an approximation, it is much more complex)

def link_to(name, options, html_options)
...
end

Now, if you invoke it this way:

link_to 'Comments', :name => 'Comments'

Ruby will get a little confused. It will try to "fill out the blanks" for you, but incorrectly:

link_to('Comments', {:name => 'Comments'}, {}) # incorrect

It will think that name => 'Comments' part belongs to options, not to html_options!

You have to help ruby by filling up the blanks yourself. Put all the parenthesis in place and it will behave as expected:

link_to('Comments', {}, {:name => 'Comments'}) # correct

You can actually remove the last set of brackets if you want:

link_to("Comments", {}, :name => "comments") # also correct

In order to use html_options, you must leave the first set of brackets, though. For example, you will need to do this for a link with confirmation message and name:

link_to("Comments", {:confirm => 'Sure?'}, :name => "comments")

Other rails helpers have a similar construction (i.e. form_for, collection_select) so you should learn this technique. In doubt, just add all the parenthesis.

蓝眼泪 2024-08-25 07:32:55

如果您想通过 Rails,我建议 content_tag (文档)。

例子:

content_tag(:a, 'Comments', :name => 'comments')

If you want to go through rails, I suggest content_tag (docs).

Example:

content_tag(:a, 'Comments', :name => 'comments')
忘东忘西忘不掉你 2024-08-25 07:32:55
<%= link_to('new button', action: 'login' , class: "text-center") %>

为 login.html ig 创建了一个锚标记

<a href="login.html" class = "text-center"> new button </a>

并供

<a href="admin/login.html" class = "text-center"> new button </a>

使用

<%= link_to('new button', controller: 'admin',
    action: 'login' , class: "text-center") %>
<%= link_to('new button', action: 'login' , class: "text-center") %>

created an anchor tag for login.html i.g

<a href="login.html" class = "text-center"> new button </a>

and for

<a href="admin/login.html" class = "text-center"> new button </a>

use

<%= link_to('new button', controller: 'admin',
    action: 'login' , class: "text-center") %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文