Browse Source

FIX 后台管理系统添加用户时,自动为用户生产登录名

pull/278/head
jasder 5 years ago
parent
commit
66fd6a71e5
3 changed files with 6 additions and 23 deletions
  1. +3
    -2
      app/controllers/admins/users_controller.rb
  2. +2
    -11
      app/forms/users/admin_create_user_form.rb
  3. +1
    -10
      app/views/admins/users/new.html.erb

+ 3
- 2
app/controllers/admins/users_controller.rb View File

@@ -68,6 +68,7 @@ class Admins::UsersController < Admins::BaseController

user = User.new(create_params)
user.type = 'User'
user.login = User.generate_login("p")
ActiveRecord::Base.transaction do
if user.save!
UserExtension.create!(user_id: user.id)
@@ -103,10 +104,10 @@ class Admins::UsersController < Admins::BaseController
end

def create_params
params.require(:user).permit(%i[login nickname gender mail phone location location_city password professional_certification])
params.require(:user).permit(%i[nickname gender mail phone location location_city password professional_certification])
end

def validate_create_params
create_params.slice(:login, :mail, :phone, :password)
create_params.slice(:mail, :phone, :password)
end
end

+ 2
- 11
app/forms/users/admin_create_user_form.rb View File

@@ -2,14 +2,13 @@ class Users::AdminCreateUserForm
include ActiveModel::Model

attr_accessor :mail, :login, :phone, :password
attr_accessor :mail, :phone, :password

validates :login, presence: true
validates :mail, presence: true, format: { with: CustomRegexp::EMAIL, message: "邮箱格式错误." }
validates :phone, presence: true, format: { with: CustomRegexp::PHONE, message: "手机号格式错误" }
validates :password, presence: true, length: { minimum: 8, maximum: 16 }, format: { with: CustomRegexp::PASSWORD, message: "8~16位密码,支持字母数字和符号" }

validate :check_login, :check_mail
validate :check_mail

private
def check_mail
@@ -20,13 +19,5 @@ class Users::AdminCreateUserForm
end
end
def check_login
return if login.blank?
if User.exists?(login: login)
raise "手机号 #{login} 已使用."
errors.add(:login, :not_exist)
end
end

end


+ 1
- 10
app/views/admins/users/new.html.erb View File

@@ -10,18 +10,9 @@
<%= f.error_notification %>
<div class="form-group px-2">
<div class="form-row">
<%= f.input :login, label: '登录名', required: true, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-md-11' } %>
</div>

<div class="form-row">
<%= f.input :nickname, label: '昵称', wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-md-11' } %>
</div>

<div class="form-row">
<%= f.input :mail, as: :email, label: '邮箱地址', required: true, error_html: { id: 'password_error'}, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-sm-11' } %>
<%= f.input :phone, as: :tel, label: '手机号', required: true, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-sm-11', autocomplete: 'off' } %>
<%= f.input :mail, as: :email, label: '邮箱地址', required: true, error_html: { id: 'password_error'}, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-sm-11' } %>
</div>
</div>

<div class="form-row">


Loading…
Cancel
Save