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

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

What's the right way to define an anchor tag in rails?

htmlruby-on-railsanchorhyperlinksegment

提问by Joe Flynn

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 :anchorto link_to.

文档(和谷歌)中可以明显看出如何生成带有段的链接,例如podcast/5#comments. 您只需为:anchorto传递一个值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.

我关心的是生成<a name="comments">Comments</a>标签的简单得多的任务,即第一个链接的目的地。

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.

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

回答by kikito

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

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

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

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

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:

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

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

Now, to your question: link_totakes twooptional hashes - one is called optionsand the other html_options. You can imagine it defined like this (this is an approximation, it is much more complex)

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

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:

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

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

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

它会认为那name => 'Comments'部分属于选项,而不是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:

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

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:

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

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.

其他 rails 助手具有类似的结构(即form_for, collection_select),因此您应该学习此技术。如有疑问,只需添加所有括号。

回答by theIV

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

如果你想通过 rails,我建议content_tagdocs)。

Example:

例子:

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

回答by Muhammad Aqeel

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

created an anchor tag for login.html i.g

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

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