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]
Related
I am trying to use Rails url_helpers inside of coffescript files. I append the .erb extension to the filename (profile.js.coffee.erb) and use ERB to access the helpers like so:
jQuery ->
window.Intl.Models.Profile = Backbone.Model.extend
url: '<%= Intl::Application.routes.url_helpers.profile_path %>'
This works great in development and it will return /profile. In production, I am using a URL root of /intl so I tell the asset pre-compiler to consider this in deploy.rb
set :asset_env, "#{asset_env} RAILS_RELATIVE_URL_ROOT='/intl'"
However, this doesn't seem to affect the url_helpers because it will still return /profile instead of /intl/profile
I tried setting ENV["RAILS_RELATIVE_URL_ROOT"] in production.rb and development.rb respectively and that didn't work either.
Any suggestions would be appreciated. Thank you.
You may need to set this at the Rack level. If you are using Passenger, you can set the RackBaseURI directive.
Same problem here.
As a partial solution I use
set :asset_env, "#{asset_env} RAILS_RELATIVE_URL_ROOT=/csc"
in deploy.rb for Capistrano deploymment. This works fine for all other aspects of the asset precompilation except this single aspect (.coffee.erb) wich seems completely unaffected by that setting.
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).
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
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.
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
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.
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.
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.