Reputation: 980
My dashboard_user controller is:
class DashboardUsersController < ApplicationController
before_action :set_dashboard_user, only: [:show, :edit, :update, :destroy]
# GET /dashboard_users
# GET /dashboard_users.json
def index
@dashboard_users = DashboardUser.all
end
# GET /dashboard_users/1
# GET /dashboard_users/1.json
def show
end
# GET /dashboard_users/new
def new
@dashboard_user = DashboardUser.new
end
# GET /dashboard_users/1/edit
def edit
end
# POST /dashboard_users
# POST /dashboard_users.json
def create
@dashboard_user = DashboardUser.new(dashboard_user_params)
#@dashboard_user.password = @dashboard_user.encrypted_password
respond_to do |format|
if @dashboard_user.save
format.html { flash[:notice] = 'User successfully Created.' and redirect_to action: "index"}
else
format.html { render :new }
end
end
end
# PATCH/PUT /dashboard_users/1
# PATCH/PUT /dashboard_users/1.json
def update
respond_to do |format|
if @dashboard_user.update(dashboard_user_params)
format.html { flash[:notice] = 'User successfully Edited.' and redirect_to action: "index"}
else
format.html { render :edit }
end
end
end
# DELETE /dashboard_users/1
# DELETE /dashboard_users/1.json
def destroy
@dashboard_user.destroy
respond_to do |format|
format.html { redirect_to dashboard_users_url, notice: 'User successfully Deleted.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_dashboard_user
@dashboard_user = DashboardUser.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def dashboard_user_params
params.require(:dashboard_user).permit(:user_id, :username, :normalized_user_name, :encrypted_password, :last_name, :first_name, :middle_name, :phone, :email, :seq_ques_id, :seq_ques_answer, :expire_password_ind, :expire_password_date, :deactivated_ind, :deactivated_date, :role_id, :created_by, :updated_by)
end
end
My dashboard_use model is
class DashboardUser < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,:rememberable, :trackable, :validatable
#require 'digest'
#before_save :encrypt_password
#def encrypt_password
# require 'digest'
#self.password = Digest::SHA1.hexdigest(self.password)
# end
attr_accessor :login
validates :username, presence: true, length: {maximum: 50 ,message: 'Exceeds Maximum number of Characters.'}, uniqueness: { case_sensitive: false }, format: { with: /\A[a-zA-Z0-9]*\z/, message: "may only contain letters and numbers." }
validates :encrypted_password, presence: {message: ' can''t be Blank!'}, length: {maximum: 50 , message: 'Exceeds Maximum number of Characters.'}
validates :last_name, presence: {message: 'can''t be Blank!'}, length: {maximum: 50, message: 'Exceeds Maximum number of Characters.'}
validates :first_name, presence: true, length: {maximum: 50, message: 'Exceeds Maximum number of Characters.'}
validates :middle_name, length: {maximum: 50 ,message: 'Exceeds Maximum number of Characters.'}
validates :phone, length: {maximum: 15 ,message: 'Exceeds Maximum number of Characters.'}
validates :email, email_format: { message: "should be like : [email protected]" }
validates :seq_ques_answer, presence: true, length: {maximum: 100,message: 'Exceeds Maximum number of Characters.'}
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["username = :value OR lower(email) = lower(:value)", { :value => login }]).first
else
where(conditions).first
end
end
end
My form.html is:
<div class="form-group">
<%= simple_form_for(@dashboard_user) do |f| %>
<% if @dashboard_user.errors.any? %>
<ul class="alert alert-danger">
<% for message_error in @dashboard_user.errors.full_messages %>
<li> <%= message_error %></li>
<% end %>
</ul>
<% end %>
<table class="mytable">
<tr>
<td class="col1">
<label for="UserName" >User Name</label>
</td>
<td class="col2">
<%= f.text_field :username %>
</td>
<td class="col1">
<label for="Password">Password</label>
</td>
<td class="col2">
<%= f.text_field :encrypted_password %>
</td>
</tr>
<tr>
<td class="col1">
<label for="LastName">Last Name</label>
</td>
<td class="col2">
<%= f.text_field :last_name %>
</td>
<td class="col1">
<label for="FirstName">First Name</label>
</td>
<td class="col2">
<%= f.text_field :first_name %>
</td>
</tr>
<tr>
<td class="col1">
<label for="MiddleName">Middle Name</label>
</td>
<td class="col2">
<%= f.text_field :middle_name %>
</td>
<td class="col1">
<label for="PhoneNumber">Phone Number</label>
</td>
<td class="col2">
<%= f.phone_field :phone %>
</td>
</tr>
<tr>
<td class="col1">
<label for="EmailID">Email ID</label>
</td>
<td class="col2">
<%= f.email_field :email %>
</td>
<td class="col1">
<label for="SecretQuestion">Secret Question</label>
</td>
<td class="col2">
<%= f.text_field :seq_ques_id %>
</td>
</tr>
<tr>
<td class="col1">
<label for="SecretAnswer">Answer</label>
</td>
<td class="col2">
<%= f.text_field :seq_ques_answer %>
</td>
<td class="col1">
<label for="Role">User Role</label>
</td>
<td class="col2">
<select id=:ROLE_ID>
<option>Select</option>
<option value="1">Admin</option>
<option value="2">User</option>
</select><br />
</td>
</tr>
</table>
<br>
<%= f.button :submit ,class: "btn btn-primary"%>
<% end %>
</div>
</div>
</div>
</div>
Now my issue is getting Password can't be blank error when I create new user.
Upvotes: 1
Views: 1159
Reputation: 3587
I got the problem. The field encrypted password is not for getting the password from the user or input end. Devise will encrypt your password and store it there. In your form.
remove the following line:
<%= f.text_field :encrypted_password %>
Now add two following lines:
<%= f.text_field :password %>
<%= f.text_field :password_confirmation %>
Make sure, your controller is permitting the required params also.
def dashboard_user_params
params.require(:dashboard_user).permit(:user_id, :username, :normalized_user_name,
:password, :password_confirmation, :last_name, :first_name, :middle_name, :phone, :email, :seq_ques_id,
:seq_ques_answer, :expire_password_ind, :expire_password_date, :deactivated_ind,
:deactivated_date, :role_id, :created_by, :updated_by)
end
and you remove validations from your model for encrypted password.
Things should work now!
Upvotes: 1
Reputation: 17834
First, you can't just enter encrypted password in the password field, so you need to remove encrypted_password
field and add password
and password_confirmation
fields. Remove
<%= f.text_field :encrypted_password %>
and add these two fields
<%= f.password_field :password %>
<%= f.password_field :password_confirmation %>
Secondly, you need to remove validation for encrypted_password
field, Remove this line from your model
validates :encrypted_password, presence: {message: ' can''t be Blank!'}, length: {maximum: 50 , message: 'Exceeds Maximum number of Characters.'}
Validation for password fields is already included in Devise's validatable
module, so you don't need to add validation for that in your model
Hope this helps!
Upvotes: 1