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.
Because the
#is unescaped, the anchor#commentFormis actually part of the URL/session/new?return_to=..., and not part of the URL/nature_photos/sdfds(which is given as the value ofreturn_toin the query string). This happens because you use the:anchoroption innew_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:
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 parameterreturn_to, it is unescaped and you can use it as a regular URL again.