Friday, August 12, 2016

How to get the current absolute URL in Rails

Absolute URL in Rails

In Rails 5, you can use:

request.original_url

Assuming, the request is:

get "/articles?page=2"

This will return:

http://www.example.com/articles?page=2

This is defined in actionpack/lib/action_dispatch/http/request.rb.

Full path without the URL

For this request:

get "/articles"

request.fullpath 

will return:

/articles

For a request with params in the URL:

get "/articles?page=2"

It will return:

/articles?page=2







No comments:

Post a Comment