Reputation: 5320
For one of my models I have a method:
def download_url url = xxxxx end
which works nicely to make /xxxx/xxxx/3
What i want to do is updated this to include an absolute URL so I can use this method in an email:
https://example.com/xxxx/xxxx/3
But I don't want to hard code. I want it to be an environment var so it works on dev & production
Upvotes: 1
Views: 279
Reputation: 7586
It may be ugly, but it's necessary. Rails apps don't and shouldn't know their root URL. That's a job for the web server. But, hardcoding sucks...
If you're using capistrano or some other deployment method, you can define the server host in a variable and write it out to a file that you can read from the app.
Upvotes: 0
Reputation: 62638
Emails are effectively views, and can use helpers. The model shouldn't really have any knowledge about the views - instead, you should use url_for
or one of its descendant methods in the email view template to generate a URL. Those helpers can generate absolute URLs based on the location that the application is running (and associated configuration - you'll want to set config.action_mailer.default_url_options[:host]
in your environment file) without having to mess with environment variables and the like.
Upvotes: 2
Reputation: 21
I would define the domain as a constant in development.rb & production.rb:
APP_DOMAIN = "https://mysite.com"
And then just use this constant in your method within the model:
def download_url
"#{APP_DOMAIN}/download/#{id}"
end
Upvotes: 0