My question is that I have a system, in this system there is the access control of users with their appropriate permissions through their profile. I use it to access the system. What I need is to be able to edit devise views so that it acts with the user model. I want to add fields like name and CPF, I get it in models but the views are recognizing by default.
I created a controller for the user.
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%>