Ask A Question

Notifications

You’re not receiving notifications from this thread.

Devise: Separate pages for account update and password

Brian Gilbank asked in Gems / Libraries

Nothing too fancy. users/edit comes from Devise. password/edit is a passwords controller I added.

Reply

Ah I am dumb, I got it working. Forgot that I needed to add the admin module in my form.

<%= form_for ["admin", @user] do |f| %>

Thank you for the quick reply.

Reply

Hey Chris - I have a follow-up question on the above issue if you don't mind.

My current set up allows users to edit their password in the traditional way, but in my dashboard/users controller, I want users to be able to update their profile, i.e. their name, email, bio, etc without a password.

This all works, but if a user fails to enter their email or name, no validation errors are displayed. I believe that Devise considers an empty field as no change, from what I can gather from the terminal output.

Is there a way to show the errors or am I going about this the wrong way?

Users Controller

module Dashboard
  class UsersController < DashboardsController
    before_action :set_user, only: [:show, :edit, :update, :destroy]
    before_action :check_admin, only: [:index]

    layout 'dashboard'

    def index
      @users = User.all
    end

    def show
    end

    def edit
    end

    def update
      if @user.update(user_params) 
       bypass_sign_in(@user)
       redirect_to dashboard_user_path(@user)
       flash[:success] = t('dashboard_pages.users_controller.messages.update_success')
     else
       render :edit
     end
    end

    def destroy
      @user.destroy
      redirect_back(fallback_location: dashboard_root_path)
      flash[:success] = t('dashboard_pages.users_controller.messages.user_deleted')
    end

    private

    def set_user
      @user = User.friendly.find(params[:id])
    end

    def user_params
      params.require(:user).permit(:username, :email, :name, :role, :about, :slug, :img)
    end


  end
end

Form partial

<%= form_with(model: [ :dashboard, @user ]) do |f| %>
<%= render partial: 'shared/form_errors', locals: {obj: @user} %>

Errors partial

<% if obj.errors.any? %>
  <div id="error_explanation">
    <h4 class="white"><%= pluralize(obj.errors.count, "error") %> prohibited this about from being saved:</h2>

    <ul class="devise__errors">
      <% obj.errors.full_messages.each do |message| %>
        <li class="white devise__errors--list"><%= message %></li>
      <% end %>
    </ul>
  </div>
<% end %>

routes

namespace :dashboard do
        root to: 'dashboards#index'
        resources :dashboards, only: [:index]
        resources :users
devise_for :users, path: '', path_names: {edit: 'user_edit', new: 'new', sign_up: 'register', sign_in: 'login', sign_out: 'signed-out', password: 'secret', confirmation: 'verification', unlock: 'unlock', invitation: 'invite', verification: 'verification', remove: 'remove', accept: 'accept' }, controllers: { omniauth_callbacks: 'users/omniauth_callbacks', registrations: 'users/registrations', masquerades: 'users/masquerades', invitations: 'users/invitations' }
get 'signed-out', to: 'signouts#index'
Reply

Devise has an user.update_without_password(params) method you can use. Might do what you need.

Reply

No affect, unfortunately. No form errors are displayed if I try and update with a blank email or name.

Reply

Brian, did you figure out a fix for the missing errors? I am facing the same problem.

Reply

I did, but I am not remembering how I did it at the moment. I believe I created another model that inherited from the User model.

Reply

Instead use update_without_password(params) I used update method, and in the line where I was validating password format I added a lambda to verify if password was nil:
validates :password, format: { with: PASSWORD_FORMAT }, on: [:create, :update], :unless => lambda {|u| u.password.nil? }

So, I created a new route to update password, and as I validate password length on devise config, everything is working well (password can not be nil on update_password).

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,400+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.