Browse Source

Streamline validation call

pull/787/head
Stéphane Meny 3 years ago
parent
commit
7333d3e16e
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 8
      app/controllers/users_controller.rb
  2. 2
      app/views/users/new.html.erb

8
app/controllers/users_controller.rb

@ -74,7 +74,8 @@ class UsersController < ApplicationController
def create
@resource = User.new(user_params.merge(org_params).merge(password_params))
if valid_attributes? && @resource.save
validate_attributes
if @resource.errors.empty? && @resource.save
redirect_to created_user_redirect_path
else
unless @resource.errors[:organisation].empty?
@ -108,15 +109,14 @@ class UsersController < ApplicationController
private
def valid_attributes?
@resource.valid?
def validate_attributes
@resource.validate
if user_params[:role].present? && !current_user.assignable_roles.key?(user_params[:role].to_sym)
@resource.errors.add :role, I18n.t("validations.role.invalid")
end
if user_params[:role].empty?
@resource.errors.add :role, I18n.t("activerecord.errors.models.user.attributes.role.blank")
end
@resource.errors.empty?
end
def format_error_messages

2
app/views/users/new.html.erb

@ -50,7 +50,7 @@
:id,
:name,
lambda { |option|
result = option.description&.sum(nil) { |hint| content_tag(:li, hint) }
option.description&.sum("") { |hint| content_tag(:li, hint) }
},
legend: { text: "Role", size: "m" } %>

Loading…
Cancel
Save