RoR: Forgot your password? Log in on the same page.

I'm trying to put the "Forgot password" field with a page on the login page, but if the user is not registered (and not in the application database), he redirects to the page of the original project "Forgot password" with errors ( http://localhost:3000/users/password ). How to make errors on the same page as on the login page ( http://localhost:3000/users/sign_in )?


In the file app / views / devise / sessions / new.html.erb

 <%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> <%= f.email_field :email, required: false, autofocus: true, placeholder: "Username" %> <%= f.password_field :password, required: false, placeholder: "Password" %> <%= f.button :submit, "Sign In", class: "btn btn-success btn-sm" %> <div class="remember-forgot"> <div class="row"> <div class="col-md-6"> <%= f.input :remember_me, as: :boolean if devise_mapping.rememberable? %> </div> <div class="col-md-6 forgot-pass-content"> <a href="javascription:void(0)" class="forgot-pass">Forgot Password</a> </div> </div> </div> <% end %> <!-- where reset is --> <div class="pass-reset"> <%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), namespace: 'forgot', html: { method: :post }) do |f| %> <%= f.error_notification %> <label>Enter the email you signed up with</label> <%= f.email_field :email, required: true, autofocus: true, placeholder: "Email" %> <%= f.submit "Submit", class: "pass-reset-submit btn btn-success btn-sm" %> <% end %> </div> 

So, there is a javascript link in which the input field will be displayed if the user forgets his sign in the credentials.

+6
source share
1 answer

obviously, two forms for the same object, having the same fields, should not be one page, you need to stay on the new page. but still I tried your question and the following steps need to be done.

1) I have to redefine the password controller for development under the user area.

 class Users::PasswordsController < Devise::PasswordsController # GET /resource/password/new def new super end # POST /resource/password def create self.resource = resource_class.send_reset_password_instructions(resource_params) if successfully_sent?(resource) flash[:notice] = "sent password" redirect_to :root else render "devise/sessions/new" end end # GET /resource/password/edit?reset_password_token=abcdef def edit super end # PUT /resource/password def update super end protected def after_resetting_password_path_for(resource) super(resource) end # The path used after sending reset password instructions def after_sending_reset_password_instructions_path_for(resource_name) super(resource_name) end end 

then my project / sessions / new page will look like this (you can add the form display logic only when, with one click, he forgot the password button. It should be simple: just add a hide class and click on the remove hide class link.)

To come in

 <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> <div class="field"> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true %> </div> <div class="field"> <%= f.label :password %><br /> <%= f.password_field :password, autocomplete: "off" %> </div> <% if devise_mapping.rememberable? -%> <div class="field"> <%= f.check_box :remember_me %> <%= f.label :remember_me %> </div> <% end -%> <div class="actions"> <%= f.submit "Log in" %> </div> <% end %> 

# assuming devise_mapping can be restored? option. you can also save the form below if the condition

 <h2>Forgot your password?</h2> <%= form_for(resource, as: resource_name, url: password_path(resource_name), namespace: "forget", html: { method: :post }) do |f| %> <%= devise_error_messages! %> <div class="field"> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true %> </div> <div class="actions"> <%= f.submit "Send me reset password instructions" %> </div> <% end %> 

you need to tell the routes to use my my password controller.

 devise_for :users, controllers: { passwords: 'users/passwords' } 

These things will cause errors to be displayed under the user login form, but the path will remain http: // localhost: 3000 / users / password . why, because we are making a page, not a redirect. rendering just displays the view without going to the controller action. now even if someone tries to send error messages to the session controller in some way (after overriding this controller), something like this

  redirect_to new_user_session_path, :messages => resource.errors 

it still will not help why, because in the # new session we initialize the resource, because this is a new action, and all errors will disappear.

I am not sure if this is satisfactory for you, or if it is not close to your requirements. I tried to cover everything. I would be happy if some reliable or official sources provide an even better answer. this will certainly increase my knowledge.

+6
source

All Articles