I'm finishing the different localizations of my site and i got a little issue in Devise email templates.
In confirmation email for instance, i have translated it all, but the link to confirm the account is auto generated using this snippet:
<%= link_to t('devise.mailer.confirmation_instructions.confirm_link'), confirmation_url(#resource, :confirmation_token => #token) %>
This auto generated link points always to my .com web version and i want it to be conditional depending on the domain (.com/.es). When the link is not auto generated i can accomplish that using:
if request.host.split('.').last == "com"
or
if request.host.split('.').last == "es"
But in this case, i don't know how i can do it.
Any suggestion?
Thanks.
You could add the :host param to your confirmation_url(#resource, :confirmation_token => #token), and look up the correct host in your translation table:
confirmation_url(#resource, :confirmation_token => #token, host: t('host'))
In your yaml file, you 'translate' the correct host for the user's language, like
en:
host: 'www.example.com'
Related
I am sending the password reset instructions via the default devise password reset instructions email. When I click on the change password it routes me to the localhost:3000 in my local machine but in production it is routing to some ip. I think it is of the cloud server we are hosting on, how to change this to the domain name ?
This is the devise password reset instructions:
<p>Hello <%= #resource.email %>!</p>
<p>Someone has requested a link to change your password. You can do this through the link below.</p>
<p><%= link_to 'Change my password', edit_password_url(#resource, reset_password_token: #token) %></p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
How to change this do I have to set the url hardcoded to the domains ? Because we have a staging and production environment.
Here I've just given a quick answer on sending emails in production in general.
In your particular case you are looking for putting the following code in production.rb:
config.action_mailer.default_url_options = { :host => 'YOUR-APP-NAME.herokuapp.com', :protocol => 'https' }
If you have more than one environment, you configure each environment separately, right? So for staging you would have something like config/environments/staging.rb and configure it there.
More details on action mailer
I've write a RoR application and I use Devise for some user management.
I've already configure my application to send email for password recovery and I use the following code in my reset_password_instruction.html.erb:
<p><%= link_to 'Modify password', edit_password_url(#resource, reset_password_token: #token) %></p>
In my config environment files I have defined: config.action_mailer.default_url_options = { :host => 'app_url.com' }.
When the application send a recovery email, it attach an url like this:
app_url.comuser insted of app_url.com/user.
What did I miss?
From the rails console, I see the url like it is suppposed to be.
<p><a href=3D"http://app_url.com/users/password/edit?reset_password_=token=3D47da8a08bd3d4asdfadfasdfa3eb9422388467adccfe095f058ca2a3352b12"=>Modify password</a></p>
I am using rails 4 and ruby 2. I have implemented login using devise. My problem is forgot password is not working on heroku but working on local. After receiving the mail when i clicked on "Change my password" link, it redirects to https://www.heroku.com/ page. How can I solve this issue? please help me if any one have any idea.
Codes:
reset_password_instructions.html.erb:
<p>Hello <%= #resource.email %>!</p>
<p>Someone has requested a link to change your password. You can do this through the link below.</p>
<p><%= link_to 'Change my password', edit_password_url(#resource, reset_password_token: #resource.reset_password_token) %></p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
Check if you configured your server's url in your config/application.rb:
config.action_mailer.default_url_options = { host: 'your.example.com' }
Find more details in the Rails Guide about Mailers.
In you config/environments/production.rb file, you should have a looking like
config.action_mailer.default_url_options = { :host => "http://eee.com" }
It defines the url used by action mailer when resolving url using the Rails url helpers. So If you set this value or change it, it should change the result.
I'm having problems when resetting a password using Devise.
When I click on 'Send me reset password instructions', I get:
ArgumentError at /users/password
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
It says that the problem is in /views/devise/mailer/reset_password_instructions.html.erb on line 5.
On line 5 in that file there is:
<p><%= link_to 'Change my password', edit_password_url(#resource, :reset_password_token => #resource.reset_password_token) %></p>
I have no idea how to setup a mailer in Rails although I have tried to. Can anybody help me with this?
Finally, you need to set up default url options for the mailer in each
environment. Here is the configuration for "config/environments/development.rb":
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
You need to specify the host so it can be shown in the confimation email.
You can create a default filter like this:
# application_controller.rb
before_filter :mailer_set_url_options
...
def mailer_set_url_options
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
I'm using ActionMailer to send a sign up confirmation email. The email needs to contain a link back to the site to verify the user, but I can't persuade Rails to generate a full URL (including the domain etc).
I'm using:
<%= url_for :controller => 'login', :action => 'verify', :guid => #user.new_user.guid, :only_path => false, :host => 'http://plantality.com' %>
in my view
Part b:
In development mode Rails gripes if I don't specify the host explicilty in the link above. But I don't want to do this in production. Any solutions?
To solve the problem to pass a host for generating URLs in ActionMailer, check out this plugin and the reason why I wrote it.
To solve the first issue, use named routes when applicable. Instead of
<%= url_for :controller => 'login', :action => 'verify', :guid => #user.new_user.guid, :only_path => false, :host => 'http://plantality.com' %>
assuming the route is called login, use
<%= login_url(:guid => #user.new_user.guid) %>
Note, I'm using login_url, not login_path.
I'm not sure if it is what you want but in config/environments/development.rb you can specify default options for mailer urls
config.action_mailer.default_url_options = {
:host => "your.host.org",
:port => 3000
}
you can do the same in config/environments/production.rb
I don't know why the previous solutions seem so complicated, but since I'm here why not give my 2 cents...
Go to /config/environments and add:
config.absolute_site_url = 'your site url'
for the respective environment (ie. in development.rb, test.rb, or production.rb). Restart web server.
This allows you to call Rails.application.config.absolute_site_url to get the desired URL. No need for plugins or weird cheat, just store the site url as an application wide variable.
I think its not 100% correct way but this can also be a solution :
See the Using asset hosts section in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}/login/?guid=#{#user.new_user.guid}"
To generate url, try this
Rails.application.routes.url_helpers.user_url(User.first.id, host: 'yourhost.io')
this will generate url like this:
http://yourhost.io/users/1
As well you can pass some params
expires = Time.now + 2.days
params = {expires: expires}
u = User.first.id
Rails.application.routes.url_helpers.user_url(u, params, host: 'host.com')
will generate:
http://yourhost.io/users/1.expires=2018-08-12+15%253A52%253A15+%252B0300
so you can werifi in action if link is not expired