在 URL Rails 路由中添加 Hash 参数

如何在 link _ to 中添加散列参数。

/p/generate/#sometext

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

link_to "Click",my_path

如何将散列参数添加到路由方法中。

现在回答我

当我发布这个问题时,我没有完全通过 url 助手 API。 Http://api.rubyonrails.org/classes/actionview/helpers/urlhelper.html#method-i-link_to

我找到了答案。

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

下面的 M. Cypher 差不多明白了:)

36370 次浏览

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.

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 Docs for details.