Ruby-on-rails 在 URL Rails 路由中添加 Hash 参数

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

Adding Hash parameter in the URL Rails Routes

ruby-on-railsroutes

提问by Manjunath Manoharan

How to add a hash parameter in link_to. I need to generate a URL something like this..

如何在 link_to 中添加哈希参数。我需要生成一个像这样的 URL..

/p/generate/#sometext

This is how my code looks now.

这就是我的代码现在的样子。

link_to "Click",my_path

How to add the hash parameter to my routes method.

如何将哈希参数添加到我的路由方法中。

Now for the answer

现在的答案

When I posted the question, I didn't got through the url helpers API fully. http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

当我发布问题时,我没有完全通过 url helpers API。 http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

I did now. :) .I found the answer.

我现在做了。:) .我找到了答案。

link_to "Click", my_path(:anchor => "sometext")

M.cypher below almost got it. :)

下面的M.cypher差点就搞定了。:)

回答by M. Cypher

This is how you would usually do it:

你通常会这样做:

link_to "Click", my_path(:anchor => "sometext")

Your routes don't have much to do with it, since the anchor part (#something) is not transferred to the server, it's a pure client-side thing.

你的路由与它没有太大关系,因为锚部分 ( #something) 没有传输到服务器,它是一个纯客户端的东西。

回答by David Vezzani

I recognize this is an old post, but I thought I would contribute my recent discovery:

我知道这是一篇旧帖子,但我想我会贡献我最近的发现:

<%= link_to "New Person", polymorphic_path([:new, person], anchor: "profile") %>

See the API Docsfor details.

有关详细信息,请参阅API 文档