Adding Hash parameter in the URL Rails Routes

Ruby on-RailsRoutes

Ruby on-Rails Problem Overview


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

/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

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

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

M.cypher below almost got it. :)

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManjunath ManoharanView Question on Stackoverflow
Solution 1 - Ruby on-RailsM. CypherView Answer on Stackoverflow
Solution 2 - Ruby on-RailsDavid VezzaniView Answer on Stackoverflow