Register only by email with devise & rails

I'm trying to implement a user authentication system similar to the one that Medium has, when the user simply puts his email address during registration and receives a confirmation link by mail, after clicking this link they are redirected back to the website and then asked to fill in the password and others data. I use Devise and found 2 articles, but none of them work. Stackoverflow has similar questions, but no good solution.

https://github.com/plataformatec/devise/wiki/How-To:-Email-only-sign-up
https://github.com/plataformatec/devise/wiki/How-To:-Override-confirmations-so-users-can-pick-their-own-passwords-as-part-of-confirmation-activation

Is there something wrong with these articles?

+5
source share
1 answer

To register a user with a password, the first thing we need to do is make sure that we have access to views like this.

rails generate devise:views

the view that I like best at the moment is app / views / devise / registrations / new.html.erb *, but you will definitely look at them, make sure they match your application *

You want to find and delete it. it should be lines 11-22 if everything is by default

  <div class="field"> <%= f.label :password %> <% if @minimum_password_length %> <em>(<%= @minimum_password_length %> characters minimum)</em> <% end %><br /> <%= f.password_field :password, autocomplete: "off" %> </div> <div class="field"> <%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation, autocomplete: "off" %> </div> 

Now the form will be sent to the controller without a password. This will result in an error. Password cannot be blank.

so what i am doing at this moment is just give it a password like this

1) copy to registrations_controller.rb

 #app/controllers/devise/registrations_controller.rb class Devise::RegistrationsController < DeviseController ... def create build_resource(sign_up_params) resource.password = SecureRandom.hex(10) # sets the password resource.save yield resource if block_given? if resource.persisted? resource.send_reset_password_instructions # send them instructions how to reset password if resource.active_for_authentication? set_flash_message! :notice, :signed_up sign_up(resource_name, resource) respond_with resource, location: after_sign_up_path_for(resource) else set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}" expire_data_after_sign_in! respond_with resource, location: after_inactive_sign_up_path_for(resource) end else clean_up_passwords resource set_minimum_password_length respond_with resource end end ... end 

I hope this helps you can find my code here

0
source

All Articles