Devise edit_password_url missing / - ruby-on-rails

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>

Related

Devise email routing to ip instead of domain name in rails

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

how to solve forgot password when working on local but not working on heroku for devise in rails 4

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.

Rails Tutorial, Hartl - ch. 10 - password/activate - Email link not redirecting to Heroku Site

The links that are sent in the password reset and account activation are being sent in the email, but when I click on them, they are not being redirected to my Heroku app and the browser gets stuck at "about:blank". Am I missing something? They look like this:
Reset password
Here is my app/views/user_mailer/password_reset.html.erb
<h1>Password reset</h1>
<p>To reset your password click the link below:</p>
<%= link_to "Reset password", edit_password_reset_url(#user.reset_token,
email: #user.email) %>
<p>This link will expire in two hours.</p>
<p>
If you did not request your password to be reset, please ignore this email and
your password will stay as it is.
</p>
Looks like the URL that you pass over to SendGrid is http://<your+heroku+app>.herokuapp.com/password_resets/..., so you'd need to make sure you specify your app's name instead of having http://<your+heroku+app>. If you're using SMTP, check to see if you have your config.action_mailer.default_url_options (in config/production.rb) setup properly:
config.action_mailer.default_url_options = { :host => 'http://appname.herokuapp.com' }
did same error and felt so LOL when I realized the solution:
check your config/environments/production.rb Listing 10.56
update host = '<your+heroku+app>.herokuapp.com' with your APP's name (in my case was host = 'twitterdave.herokuapp.com' )
Let me know if it helps

Devise - How to translate auto generated links

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'

Problems With Resetting Password - Devise

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

Resources