Rails 3.1.3 使用带有 link_to 标记的锚属性从 posts/index 到 posts/show/id 不起作用

发布于 2025-01-04 11:10:53 字数 1093 浏览 5 评论 0原文

我在 posts/index 视图上使用 link_to 标签,并希望使用锚点将其链接到 posts/show/id 视图,使其向下滚动到评论表单。由于某种原因,我无法让锚点工作。这是我的代码:

在 posts/index

<%= link_to 'Add a Comment', post, :anchor => 'comment_form' %>

这无法将 # 符号附加到链接末尾,因此它只是 localhost:3000/posts/id。 我还尝试了 link_to 的许多变体,包括:

<%= link_to 'Add a Comment', post(:anchor => 'comment_form' %>

<%= link_to 'Add a Comment', :controller => 'posts', :action => 'show', :id => @post, :anchor => 'comment_form' %>

但我没有运气。

这是我的 posts#show 操作:

  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

这是我希望锚点滚动到的 posts/show 视图:

<h2><a name="comment_form" id="comment_form">Add a comment:</a></h2>

此外,如果我链接到索引页面上的某些内容,则上述任何操作都有效,因为我可以看到哈希#已附加到输出的 url 中。由于某种原因,尝试链接到显示页面时它不起作用。有什么帮助吗?

I am using a link_to tag on my posts/index view and want to link it to my posts/show/id view with an anchor that makes it scroll down to the comments form. For some reason I can't get the anchor to work. Here is my code:

In posts/index

<%= link_to 'Add a Comment', post, :anchor => 'comment_form' %>

This fails to append the # sign to the end of the link, so it is just localhost:3000/posts/id.
I have also tried many variations for link_to, including:

<%= link_to 'Add a Comment', post(:anchor => 'comment_form' %>

and

<%= link_to 'Add a Comment', :controller => 'posts', :action => 'show', :id => @post, :anchor => 'comment_form' %>

but I've had no luck.

Here is my posts#show action:

  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

and here is the posts/show view where I want the anchor to scroll to:

<h2><a name="comment_form" id="comment_form">Add a comment:</a></h2>

Furthermore, any of the above works if I am linking to something on the index page, as I can see the hash # has been appended to the outputted url. For some reason it is not working when trying to link to the show page. Any help with this?

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

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

发布评论

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

评论(1

凝望流年 2025-01-11 11:10:53

试试这个:

link_to('Add a comment', post_path(post, :anchor => 'comment_form'))

link_to 的第二个参数通常按原样传递给 url_for,第三个参数用作 的属性哈希。最终生成的元素。

因此,在第一个示例中,您传递一个 Post 对象作为第二个参数,传递一个哈希作为第三个参数。只有 Post 会传递给 url_for。它永远看不到包含 :anchor 选项的哈希,因此您不会在生成的 URL 末尾看到锚点。 (但是您可能会在生成的 元素上看到 anchor="comment_form" 属性。)

您的第二个示例在语法上不正确。我想这会导致错误。

你的第三个例子......应该有效。我不知道为什么没有:-)

Try this:

link_to('Add a comment', post_path(post, :anchor => 'comment_form'))

The second argument to link_to is typically passed as-is to url_for, and the third argument is used as an attributes hash for the <a> element that ultimately gets generated.

So in your first example, you're passing a Post object as the second argument and a hash as the third argument. Only the Post would be passed to url_for. It never sees the hash containing the :anchor option, so you wouldn't see the anchor at the end of the generated URL. (But you would probably see an anchor="comment_form" attribute on the generated <a> element.)

Your second example is syntactically incorrect. I imagine that resulted in an error.

Your third example...should've worked. I'm not sure why it didn't :-)

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