Skip to main content
1 de 2

Editar views devise

A minha questão é que tenho um sistema, nesse sistema há o controle de acesso de usuários com suas devidas permissões mediante seu perfil. Uso o devise para tal acessar o sistema. O que necessito é poder editar as views do devise para que esta atue com o model do usuário. Quero adicionar campos como nome e cpf, consigo no models mas a views fica reconhecendo a padrão.

Criei um controller para usario.

controller:

class Admin::UsersController < AdminController
    before_action :set_user, only: [:show, :edit, :update, :destroy]

  def index
    @users = User.all
  end

  def new
    @user = User.new
    @profiles = Admin::Profile.all
  end

  def edit
    
  end

  def show
    render json: @user
    @users = User.page(params[:page])
  end

  def create
    @user = User.new(user_params) 

    respond_to do |format|
      if @user.save
        format.html { redirect_to "/admin/users", notice: 'Usuário criado com sucesso.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to "/admin/users", notice: 'Usuário atualizado com sucesso.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to "/admin/users", notice: 'Usuário Alvo excluido com sucesso.' }
      format.json { head :no_content }
    end
  end

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

    def user_params
       params.require(:user).permit(    
                                      :name,
                                      :email,
                                      :password,
                                      :password_confirmation,
                                      :profile_id
                                    )
    end
end

models:

class User
  include Mongoid::Document
  include Mongoid::Search

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  ## Database authenticatable
  field :name,               type: String, default: ""
  field :email,              type: String, default: ""
  field :encrypted_password, type: String, default: ""
  belongs_to :profile, class_name: "Admin::profile"
  
  ## Recoverable
  field :reset_password_token,   type: String
  field :reset_password_sent_at, type: Time

  ## Rememberable
  field :remember_created_at, type: Time

  ## Trackable
  field :sign_in_count,      type: Integer, default: 0
  field :current_sign_in_at, type: Time
  field :last_sign_in_at,    type: Time
  field :current_sign_in_ip, type: String
  field :last_sign_in_ip,    type: String

  ## Confirmable
  # field :confirmation_token,   type: String
  # field :confirmed_at,         type: Time
  # field :confirmation_sent_at, type: Time
  # field :unconfirmed_email,    type: String # Only if using reconfirmable

  ## Lockable
  # field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
  # field :unlock_token,    type: String # Only if unlock strategy is :email or :both
  # field :locked_at,       type: Time
  
end