在 rails 中定义锚标记的正确方法是什么? [英] What's the right way to define an anchor tag in rails?

查看:20
本文介绍了在 rails 中定义锚标记的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我关心的是生成 Comments</a> 标记(即第一个链接的目的地)这一更简单的任务.

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.

推荐答案

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

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

当 ruby​​ 函数接受一个散列参数时:

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 会填空并理解您要完成的任务是:

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

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

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

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 会有点困惑.它会尝试为您填空",但错误地:

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

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

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

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

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

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

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

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")

其他 rails helper 也有类似的结构(即 form_forcollection_select),所以你应该学习这个技巧.如有疑问,只需添加所有括号.

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.

这篇关于在 rails 中定义锚标记的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆