How to edit Devise Views?

Asked

Viewed 911 times

1

My point is that I have a system, in this system there is access control of users with their proper permissions upon their profile. I use Devise to access the system. What I need is to be able to edit the views of Vise to act with the user’s model. I want to add fields like name and CPF, with you in models but the views keep recognizing the pattern.

I created a user controller.

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

Model:

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

Views:

<%= simple_form_for [:admin, @user], html: { class: 'form-horizontal' } do |f| %>
  <%= f.error_notification %>
  <%= f.input :name, label: 'Nome' , :required => :true %>
  <%= f.input :email, label: 'Email' , :required => :true %>
  <%= f.input :cpf, label: 'Cpf' , :required => :true %>
  <%= f.input :password, label: 'Password' , :required => :true %>
  <%= f.input :password_confirmation, label: "Confimar password" , :required => :true %>
  <%= f.input :profile, collection: @profiles, label: "Perfil" , :required => :true %>
  <div class="pull-right">
  <%= f.button:submit, "salvar",:class =>"btn btn-success"  %></div>
<%end%>
  • What do you mean in Views you are recognizing the patterns? You can add the Views code?

  • It recognizes Developer’s own views and I’d like to edit it so it gets my models' fields

2 answers

1

To be able to edit Devise Views you will execute the following command:

rails generate devise:views

This will generate customizable views within your project, so you can change them however you want. More details you can consult the documentation of the Devise which deals with this type of procedure

0

a views:

<%= simple_form_for [:admin, @user], html: { class: 'form-horizontal' } do |f| %>
  <%= f.error_notification %>
  <%= f.input :name, label: 'Nome' , :required => :true %>
  <%= f.input :email, label: 'Email' , :required => :true %>
  <%= f.input :cpf, label: 'Cpf' , :required => :true %>
  <%= f.input :password, label: 'Password' , :required => :true %>
  <%= f.input :password_confirmation, label: "Confimar password" , :required => :true %>
  <%= f.input :profile, collection: @profiles, label: "Perfil" , :required => :true %>
  <div class="pull-right">
  <%= f.button:submit, "salvar",:class =>"btn btn-success"  %></div>
<%end%>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.