views:

600

answers:

1

Hi,

The following link

<%= link_to "Login to comment", :url => new_session_url(:return_to => request.request_uri, :anchor => 'commentForm'), :method => :get, :html => { :id => 'login_to_comment'} %>

generates the following url:

http://localhost:3000/session/new?return_to=%2Fnature_photos%2Fsdfds#commentForm

and the parameters are as below from the logs.

Processing SessionsController#new (for 127.0.0.1 at 2009-07-16 02:04:44) [GET]
Parameters: {"return_to"=>"/nature_photos/sdfds", "action"=>"new", "controller"=>"sessions"}
Completed in 74721ms (View: 15, DB: 0) | 200 OK [http://localhost/session/new?return_to=%2Fnature_photos%2Fsdfds]

Here I need that #commentForm anchor is excluded from the parameter return_to

I need to get that value to scroll down to the comment form at the bottom of the page.

+3  A: 

Because the # is unescaped, the anchor #commentForm is actually part of the URL /session/new?return_to=..., and not part of the URL /nature_photos/sdfds (which is given as the value of return_to in the query string). This happens because you use the :anchor option in new_session_url: it will add the anchor to the complete URL. Note that the anchor is never sent to the server!

What you are probably looking for is the following:

:url => new_session_url(:return_to => request.request_uri + "#commentForm"), ...

This will append the anchor directly to the URL that is the value of return_to, which implies that the # character should be URI escaped to %23. When it is sent back to the server as the parameter return_to, it is unescaped and you can use it as a regular URL again.

molf