Chris
Chris

Reputation: 1660

How to use different routes for sections of the same page?

I am developing in Rails 3 and have a html document with the following structure:

<div id="one">

</div>

<div id="two">

</div>

<div id="three">

</div>

I can link to sections by going to the address: /index#one, /index#two and /index#three. Is it possible to give these a specific named route in rails? Something like:

match '/one',   :to => 'pages#index#one'
match '/two',   :to => 'pages#index#two'
match '/three', :to => 'pages#index#three'

I'd rather actually name the route, than put the hash into the address bar, if that makes sense.

Thanks in advance.

Upvotes: 1

Views: 861

Answers (1)

M. Cypher
M. Cypher

Reputation: 7066

In the views, you can use the :anchor parameter to modify the named routes, like this:

<%= link_to 'One', pages_path(:anchor => 'one') %>

That's how I would do it.

Edit: I think I misunderstood your problem. You want completely distinct URLs which redirect to different anchors on an identical page, right?

Personally, I don't think it's a good idea, since if you have links to the different sections in the document, they will produce a page reload which is normally not necessary for anchors. You'd also need some JavaScript workaround for behavior which is automatically handled by the browser if done the usual way.

Upvotes: 3

Related Questions