Full current url with url_for()

How to obtain the current url with url_for()?

For example current url is: http://aaa/bbb?c=123#45

Problem is, whenever I call url_for(), parameters and anchor are not
included. Using the example above, only received: http://aaa/bbb.
?c=123#45 is always omitted.

Doesn’t have to be absolute. Relative url is fine. This is for
redirecting back to refferer purposes.

Thanks.

Ack should have used request_uri() for this.

But even request_uri() cannot capture the anchor.

Current url: http://aaa/bbb?c=123#45
request_uri: http://aaa/bbb?c=123

Any idea on getting that anchor bit?

Hmm, is the anchor something that the webserver reads or just something
the browser understands? It might be that the server doesn’t even see
it, just a guess though.

It may be possible with javascript? But that’s not exactly useful is it.

Here’s the documentation for url_for:

There’s an “anchor” option. Also, for the c=123 in the query string,
just
use :c => 123 in url_for. The full call would look like

url_for :controller => ‘aaa’, :action => ‘bbb’, :c => ‘123’, :anchor =>
‘45’

neilhulluni wrote:

Hmm, is the anchor something that the webserver reads or just something
the browser understands? It might be that the server doesn’t even see
it, just a guess though.

It may be possible with javascript? But that’s not exactly useful is it.

env[‘HTTP_REFERRER’] returns the anchor find so the server is not the
problem. But HTTP_REFERRER only works if you actually click on a link.

I need this on server side so am afraid javascript won’t help.

Daniel H. wrote:

Here’s the documentation for url_for:
ActionController::Base

There’s an “anchor” option. Also, for the c=123 in the query string,
just
use :c => 123 in url_for. The full call would look like

url_for :controller => ‘aaa’, :action => ‘bbb’, :c => ‘123’, :anchor =>
‘45’

neilhulluni wrote:

I thought he was trying to find his current url not generate
one…hence the request_uri method…?

Yup. Just want to pass the current url (so can redirect back later).

Not necessarily has to be url_for(). I guess the this thread title is
not correct. (I would edit the title if I could.)

I thought he was trying to find his current url not generate
one…hence the request_uri method…?

hmm, im getting similar problems, i have the following url im trying to
save:

http://127.0.0.1:3000/photos/view?book=1&id=2

The problem is, when I use url_for {request.parameters},

it doesnt include the ?book=1&id=2 part. However, when I do “puts
request.parameters[:id]”, I get “2”

Whats going on here? I too am trying to save the incoming url for
redirecting later.

You don’t need to save the URL, just the parameters hash. My current(and
first real) project does basically what they do in AWDWR:

session[:jumpto] = request.parameters

in a before_filter for the pages I want to be able to jump back to.
Works great.

Jason

oh thats right, you can pass a hash into redirect_to…awesome! works
great!