diff --git a/app/controllers/admins/project_licenses_controller.rb b/app/controllers/admins/project_licenses_controller.rb index 5c16a884b..44ae75118 100644 --- a/app/controllers/admins/project_licenses_controller.rb +++ b/app/controllers/admins/project_licenses_controller.rb @@ -1,6 +1,6 @@ class Admins::ProjectLicensesController < Admins::BaseController before_action :set_license, only: [:edit,:update, :destroy,:show] - before_action :validate_params, only: [:create, :update] + # before_action :validate_params, only: [:create, :update] def index sort_by = License.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'created_at' @@ -30,13 +30,12 @@ class Admins::ProjectLicensesController < Admins::BaseController # position: max_position # } @project_license = License.new(license_params) - - if @project_license.save! + if @project_license.save redirect_to admins_project_licenses_path flash[:success] = "创建成功" else - render :new - flash[:danger] = "创建失败" + redirect_to admins_project_licenses_path + flash[:danger] = @project_license.errors.full_messages.join(",") end end @@ -54,12 +53,13 @@ class Admins::ProjectLicensesController < Admins::BaseController # permissions: permissions.to_s, # limitations: limitations.to_s # } - if @project_license.update_attributes(license_params) + @project_license.attributes = license_params + if @project_license.save redirect_to admins_project_licenses_path flash[:success] = "更新成功" else - render :edit - flash[:danger] = "更新失败" + render admins_project_licenses_path + flash[:danger] = @project_license.errors.full_messages.join(",") end end @@ -98,23 +98,23 @@ class Admins::ProjectLicensesController < Admins::BaseController params.require(:license).permit(:name,:content) end - def validate_params - name = params[:license][:name] - if name.blank? - flash[:danger] = "名称不允许为空" - redirect_to admins_project_licenses_path - elsif check_license_present?(name) && @project_license.blank? - flash[:danger] = "创建失败:名称已存在" - redirect_to admins_project_licenses_path - end - end + # def validate_params + # name = params[:license][:name] + # if name.blank? + # flash[:danger] = "名称不允许为空" + # redirect_to admins_project_licenses_path + # elsif check_license_present?(name) && @project_license.blank? + # flash[:danger] = "创建失败:名称已存在" + # redirect_to admins_project_licenses_path + # end + # end - def check_license_present?(name) - return true if name.blank? - name_downcase = name.downcase - name_upcase = name.upcase - name_first_big = name.capitalize - License.exists?(name: name_downcase) || License.exists?(name: name_upcase) || License.exists?(name: name_first_big) - end + # def check_license_present?(name) + # return true if name.blank? + # name_downcase = name.downcase + # name_upcase = name.upcase + # name_first_big = name.capitalize + # License.exists?(name: name_downcase) || License.exists?(name: name_upcase) || License.exists?(name: name_first_big) + # end end diff --git a/app/models/license.rb b/app/models/license.rb index 0a14fb85e..676254717 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -11,4 +11,8 @@ class License < ApplicationRecord include Projectable + + validates :content, presence: true + validates :name, uniqueness: true, presence: true + end diff --git a/config/locales/zh-CN.yml b/config/locales/zh-CN.yml index 62ea16966..95b7c429b 100644 --- a/config/locales/zh-CN.yml +++ b/config/locales/zh-CN.yml @@ -223,6 +223,9 @@ zh-CN: platform: '直播平台' live_time: '开播时间' duration: '直播时长' + license: + name: '许可证名称' + content: '许可证内容' close_pr: 合并请求 roles: Developer: 开发者