0

As my reputation is lower than 50, so Im not able to comment below the accepted answer in this post In Rails Devise gem how to modify the send_reset_password_instructions method? for more information.

I want to customize recoverable.rb in devise. I made a copy of it in my folder with path lib/devise/models/recoverable.rb. The problem is when request to send reset password instruction, I got error undefined method activerecord51? for Devise:Module. How do i solve this?

It seems my recoverable is not in Devise module. I tried a bit by making a copy of devise.rb in lib/ folder. But it doesn't help.

Can someone please help?

EDIT

Sorry for any inconvenience. At the moment Im just trying to pass more opts to the method send_reset_password_instructions.

Any idea about this?

1 Answers1

1

How about do it in some rails initializer? Your are possibly overwriting the original class/module so all the other methods are gone.

# config/initalizers/devise.rb
Devise::Models::Recoverable::ClassMethods.module_eval do
  def send_reset_password_instructions(your, params)
    token = set_reset_password_token
    send_reset_password_instructions_notification(token)

    token
  end
end
Denny Mueller
  • 3,505
  • 5
  • 36
  • 67
  • Thanks for your response. What if i want to use the original method inside devise like generate token? – Nhan Nguyen Jul 24 '18 at 11:23
  • Well thats a different question. But you could incorporate both methods into your overwrite. But since its a overwrite its well... overwritten. You could try not to overwrite it and add a separate method into `Devise::Models::Recoverable`. So you have something like `Devise::Models::Recoverable.method_name` and `Devise::Models::Recoverable.my_method_name`. – Denny Mueller Jul 24 '18 at 11:28
  • Hi @Denny Mueller, i dont really get that. Btw, i updated my question. Does it help you understand more about my problem? Thanks for your time anyway. – Nhan Nguyen Jul 25 '18 at 06:50
  • updated my answer. this is one way how you overwrite a specific method in the original module. And keep the rest of the module intact. If you put `module Devise::Models::Recoverable` in the the lib folder then you overwrite the whole module and everything else you dont copy over is gone. – Denny Mueller Jul 25 '18 at 07:02
  • Just gave it a try, put your code in `config/initalizers/devise.rb` but got error `wrong number of arguments (2 for 1): App 1230 stdout: (gem) devise-2.2.3/lib/devise/models/recoverable.rb:108:in `send_reset_password_instructions'`. Perhaps the method is not overwritten yet – Nhan Nguyen Jul 25 '18 at 07:21
  • Don't knwo if its overwritten, should be. Have you tried to checkit with `binding.pry` or some `puts`? – Denny Mueller Jul 25 '18 at 07:43
  • I found the difference here. Your are talking about `send_reset_password_instructions` of **Recoverable** module, but mine belong to **ClassMethods** module. So i change your code a bit to `Devise::Models::Recoverable::ClassMethods.module_eval do` and it works. Then the another problem comes relate to method `set_reset_password_token` which is a _protected_ method. How about this? – Nhan Nguyen Jul 25 '18 at 08:22
  • Hi @denny-mueller, i fixed on my own with some custom code. Your answer actually tell me the right direction, so please update a bit as i suggest above (**Devise::Models::Recoverable::ClassMethods.module_eval do**) for anyone come later, i can mark yours as accepted answer then – Nhan Nguyen Jul 25 '18 at 09:37
  • AH ok there are 2 `send_reset_password_instructions` one instance and one class method. Updated my answer. – Denny Mueller Jul 25 '18 at 09:59