The base URL of the Rails application in the model

To find out if the User has shared my Facebook page, I want to be able to create the following URLs:

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

Where http://stylehatch.co should be my base URL. I am thinking of having a method in the User model that will create and return this URL, but I cannot access root_url from my model.

How to get the base URL of my application from the model?

Tell me if my urls look like this:

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

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

  include Rails.application.routes.url_helpers 

in my model, but it seems that root_url is zero. Any thoughts? Is it right to be a model method or should I put it in an assistant?

thanks

+7
source share
2 answers

You can use environment variables:

in the environment

 ROOT_URL=http://myapp.com 

in-app

 ENV['ROOT_URL'] 
+1
source

If you want to get the root URL in your model, then I made an ENV variable call.

If you have not already done so, go ahead and create .env in the root directory of your application and install in development:

 ROOT_URL=http://localhost 

In your work environment:

 ROOT_URL=https://mydomain.com 

Of course, this is hard-coded, so the error is that you need to remember to change this when changing domains, and each environment file should be different.

And make sure this is also in your gitignore, as other important data is stored here.

In your model, you call: ENV ['ROOT_URL']

+14
source

Source: https://habr.com/ru/post/943986/


All Articles