Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base URL of Rails app in Model

In order to be able to see if a User has shared my page on Facebook, I want to be able to create this kind of URLs:

http://graph.facebook.com/?id=http://stylehatch.co/some_unique_user_token

Where http://stylehatch.co would need to be my base URL. I am thinking on having a method in the User model that will build and return that URL, but I can't access root_url from within my model.

How can I get the base URL of my application from a Model?

Say if my urls look like this:

http://myapp.com/users/new

http://myapp.com/users/2/show

How can I get the "http://myapp.com" from my model? I have added:

  include Rails.application.routes.url_helpers

in my model, but it seems that root_url is nil. Any thoughts? Is this correct to be a Model method, or should I place it in a helper?

Thanks

like image 590
Hommer Smith Avatar asked Apr 30 '13 17:04

Hommer Smith


Video Answer


1 Answers

You may use environment variables:

in environment

ROOT_URL=http://myapp.com

in-app

ENV['ROOT_URL']
like image 175
Danil Speransky Avatar answered Nov 02 '22 19:11

Danil Speransky