Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run rails Devise method edit_password_url by hand?

We have a user whose mail provider seems to be blocking the account from which we send the password reset emails.

I wanted to just get the reset-password-URL running from irb, and mail it by hand. I can't seem to figure out how to run this thing "edit_password_url" or where it lives or in what scope it is defined.

Any tips on how to generate a reset-password url for a user by hand in irb?

like image 726
Eric Weaver Avatar asked Oct 18 '13 18:10

Eric Weaver


3 Answers

You can do it through the console with a little bit of work. Here is how I approached it:

start rails console in your terminal:

$rails c

I looked at the devise mailer view to see what it was calling to create the reset password URL:

<p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @token) %></p>

The @resource in this code is your user, and the @token is their reset password token

Find your user by id, email or whatever. Then find their reset password token:

u = User.find(1)
token = u.reset_password_token

To get access to the view you will need to create an instance of ActionView::Base

view = ActionView::Base.new

I then tried to access the url helper, but devise complains about

NoMethodError: undefined method `main_app' for #<ActionView::Base>

So I had to type a method into the console to fix that error (see this):

def main_app
  Rails.application.class.routes.url_helpers
end

Depending on whether you have your mailer configured correctly in the rails console environment you are in, you may get some errors about the :host param not being set up. To avoid this you could just call _path instead of _url. Now you can call the url helper and pass the variables you set for user and token:

edit_password_path(u, :reset_password_token => token)

=> /users/password/edit?reset_password_token=123

The short answer is that you need to find their reset_password_token and append it to this URL:

http://yourdomain.com/users/password/edit?reset_password_token=<password-token-here>
like image 119
johnmcaliley Avatar answered Oct 19 '22 22:10

johnmcaliley


In Devise 3.3, I did the following:

$ bin/rails c
> include Devise::Controllers::UrlHelpers
like image 20
dteoh Avatar answered Oct 19 '22 23:10

dteoh


I recently found myself in a similar spam-folder situation.

This snippet worked nicely for me with rails 4, devise 4.3.

It also sends the reset password email, but for the sake of simplicity I think that's fine.

user = User.find(123)

token = user.send_reset_password_instructions

Rails
  .application
  .routes
  .url_helpers
  .edit_user_password_url(user, reset_password_token: token)
like image 1
gondalez Avatar answered Oct 19 '22 21:10

gondalez