views:

1741

answers:

4

How can I get current absolute URL in my Rails view?

The request.request_uri gives me only relative URL.

A: 

When you say only relative, do you mean just without the domain?

If so, look into request.domain

ghoppe
+16  A: 

You want request.url instead of request.request_uri. This combines the protocol (usually http://) with the host, and request_uri to give you the full address.

Jaime Bellmyer
+1  A: 

I think request.domain would work, but what if you're in a sub directory like blah.blah.com? Something like this could work:

<%= request.env["HTTP_HOST"] + page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>

Change the parameters based on your path structure.

Hope that helps!

James M
Yes Jaime's answer is way better, but if you want to be really inefficient, you could do it my way.
James M
+3  A: 

This current_url method returns the current url and allows to merge in other params

current_url --> http://...
current_url(:page=>4) --> http://...&amp;page=4
grosser