Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: how to get the current host, or build a URL with it? [duplicate]

I need to use the host to build a URL with a different port.

For example, if the host is example.com, I need to generate a URL like http://example.com:8080/

I need it to be portable, so when I'm in my local enviroment it shows http://localhost:8080/ instead.

Any ideas?

like image 559
HappyDeveloper Avatar asked Feb 26 '12 12:02

HappyDeveloper


People also ask

What is the best way to get the current request URL in Rails?

You should use request. original_url to get the current URL.

How session works in rails?

The unique ID generated by your server is stored on the server-side and in the user's browser as a cookie. Then each time a user checks a new page on your server, the unique id is compared between your server's key and it matches the two.

How can you create a new Rails project?

Run RubyMine and click New Project on the Welcome Screen. Name: specify a name for the project (rails-helloworld in our case). Location: specify a path to the directory in which you want to create the project. By default, RubyMine creates a directory with the same name as the project.


1 Answers

I often use a before_filter in ApplicationController to set an instance variable with the host name, something like this:

@hostname = request.host || "www.mydomain.com" 

You can also use request.port to get the port number which the request came through (taken from the HTTP headers).

like image 143
Alex D Avatar answered Sep 21 '22 17:09

Alex D