When sending emails (for example for 'Reset password instructions') using Devise, i need to know current domain URL, and set that value into mailer's template.
request.url doesn't work for me.
Assuming, this Rails application is accessibly from multiple URLs.
Any ideas?
Request object is not available in the mailers. You will need to set up the host in the environment configuration file with something like:
ActionMailer::Base.default_url_options[:host] = 'myhost.com'
Related
In my requirement ,there are different domain names for single project.I would like to send reset password link with a domain name where the user requested for the reset password.For that I have done the follows in application_controller.rb and it works well.
before_filter :set_mailer_host
def set_mailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
$url_host = request.host_with_port
end
Later,I have used sidekiq for delayed mailer and updated like follows on entire application
Notifier.delay.password_reset(user.id)
As the mailer was configured with sidekiq it was using default domain name provided in configuration only and it was not using the dynamic domain name provided from request on application_controller.rb
Any suggestion to get the dynamic domain on mailers running by sidekiq would be greatly appreciated.
Rails - 3.2.8
Ruby - 1.9.3
sidekiq - 2.13.1
Thanks!!!
One of the options would be to associate each user with a domain and use this information when sending the email in sidekiq.
You'll need to construct the url yourself rather than rely on AM to do it for you.
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
In my previous question, I wanted to get the base url from api subdomain, eg.
api.test.com
this can be done by
request.domain
However, if I have different environment as subdomain, say
api.dev.test.com, api.beta.test.com, api.staging.test.com...
then request.domain will give me
test.com
instead of
dev.test.com, beta.test.com....etc.
Any solution to get the proper domain? root_url, request.host, request.referer won't work in this case. Should I check the environment and set the domain/host in some configuration file when loading?
Thanks.
From the Rails doc....
domain(tld_length = ##tld_length)
Returns the domain part of a host, such as “rubyonrails.org” in “www.rubyonrails.org”. You can specify a different tld_length, such as 2 to catch rubyonrails.co.uk in “www.rubyonrails.co.uk”.
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.
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.