在我的应用中,用户最多可以创建两个别名。我知道如何构建对html的检查,这将禁用创建另一个别名的选项,如果他们已经有两个。但是,如何在模型中构建此限制?
编辑:我知道我可以在表单上添加条件,但我想将条件放入我的控制器中。这就是我想要弄清楚的。
答案 0 :(得分:0)
使用count
。如果用户组的数量大于或等于2,则阻止创建新组。
答案 1 :(得分:0)
在别名模型中,添加自定义验证以计算当前用户的别名数:
class UsersController < ApplicationController
# include actions pertain to users model
end
class IssuesController < ApplicationController
def index
@issue = Issue.all
@issue = @issue.paginate(page: params[:page], per_page:10)
@issue = @issue.order("created_at DESC")
end
end
class ProductionReportsController < ApplicationController
def new
@issue = Issue.find(params[:id])
# if you were to use belongs_to and has_many design, I mentioned above
@production_report = @issue.production_reports.build
# or not
@production_report = ProductionReport.new
end
def create
@issue = Issue.find(params[:id])
# if you were to use belongs_to and has_many design, I mentioned above
@production_report = @issue.production_reports.build(production_report_params)
# or not
@production_report = ProductionReport.new(production_report_params)
if @production_report.save
flash[:notice] = "production Saved Successfully!!!"
redirect_to issues_url
else
# logic for failed save, repopulate the form with validation errors
end
end
end