in my config/application.rb file I have a line:
config.action_mailer.default_url_options = { :host => 'example.com' }
How can I configure the host attribute for default url options automaticaly from the domain, where my Rails app is hosted ?
It has to be explicitly specified somewhere because how else would rails know what the domain is?.. A request through the browser (or curl, or whatever) will have a host which rails knows about, but if you load up rails console, what would the domain be?
If you're using something like capistrano, you could write a task to create an initializer which sets the mailer url depending on the host(s) you've set in your deploy recipe.
Related
I want to place an image in emails to let me know if a person has opened the email. So the image will simply be a route in my rails app to a specific controller to handle that logic:
<img src="http://www.example.com/invitations/43/open.png" />
Problem is, http://www.example.com works in production, but we have different development and test environments. Is there a way to use an environment configuration value in the /config/environments Ruby files inside an ActionMailer template?
You could make use of the action_mailer.default_url_options if you set it in your config files (config/environnments/).
If you set in production.rb and developpment.rb like so
config.action_mailer.default_url_options = { host: 'www.example.com' }
You can call it anywhere using
Rails.application.config.action_mailer.default_url_options[:host]
I have a mailer template in rails. I am trying to use the url of a paperclip attachment: <%=item.picture.url%>
OFcourse, this only renders the path which is pretty useless in an email. I need to get the absolute url.
Also since I am using rake to run the task, there is no request per se.
I remember setting the default host in
config.action_mailer.default_url_options = { :host => 'sitename.com' }
How do I access this value in the mailer template?
you can use helper in action-mailer and absolute url helper is root_url.
Try this
You can get the host name using the below line of code
request.host
I just deployed one of my apps to heroku. This app uses :
A default "myapp.herokuapp.com" address,
And I got a domain configured so that the app can be reached through "www.myapp.com".
And I noticed today the following issue : my application links are based on "http://myapp.herokuapp.com" domain (hence I get "http://myapp.herokuapp.com/page" URLs) even when I access the app using my domain name (I would then expect to get "www.myapp.com/page" URLs).
I tried to edit my production.rb and set the default_url_options :
# Base domain for url generation
config.action_controller.default_url_options = { :host => "www.myapp.com" }
But it doesn't change a thing. Also tried to change this in application.rb, just in case, but nothing happens either.
Any clue ?
Thanks a lot for your help guys !
Edit : This used to work as expected before today when I did the database migration to the new Heroku postgres thing. Don't know if this can have any impact.
If you're using _path methods for your urls, this is generating a relative path which is always based on the url you visit. If you're using controller/fragment caching, you should probably use _url instead in your views. You might also want to consider setting config.action_controller.perform_caching to false in your production.rb if all your pages have some controller logic.
See this page for more info on how caching works in Rails.
I had a similar problem. It was caused by the following line of code which was pointing to heroku.com and getting redirected to herokuapp.com
config.action_mailer.default_url_options = { :host => 'my-staging-domain.heroku.com' }
I mention it because it's the action_mailer.default_url_options yet clearly it affects the default url options outside of the scope of the mailer if you haven't explicitly set up the action_controller.default_url_options
Whenever I send out emails in development mode, it does not put the localhost:3000 in the url. Is there any way I can configure this site-wide?
I have tried the following:
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
inside of an initializer, but it has no effect.
I have also tried this in 'development.rb':
config.action_mailer.default_url_options = {:host => 'localhost:3000'}
Help?
(as in the comments to the question, so that the question can be answered)
Make sure you use the _url helpers in your Mailer views, because _path will only output relative urls (i.e. without the hostname).
i want to send an email to user after he sign-up with code.for ex
http://192.168.1.51:3000/logins/activate/435546dgfd757676657 #link contains in an email
how can i create the above URL in my notifier model.
i know following way
url_for :controller=>'logins', :action=>'activate', :id=>'435546dgfd757676657' , :host=>'http://192.168.1.54:3000'
Which is working properly.
what i want that host should not be hard coded. How can i get host with port in a model.
In controller i can find it using follwing ways
request.host_with_port
Please provide me correct ruby way for doing same.
You can define the host in your environment.rb file.
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
As your host probably changes depending of your environment (development, test, production), it's better to put that config line inside the environment file.
After that, every link in emails will be made with that host. You don't have to provide it in the view anymore.