【发布时间】:2017-11-27 03:33:00
【问题描述】:
我正在使用一个非常简单的博客应用程序来学习 Ruby on Rails,并且我刚刚获得了活跃的管理员来工作。在添加新类别时,我不断得到 ActiveRecord::RecordNotUnique 在 Admin::CategoriesController#create。
类别表中已存在记录,因此存在违规行为。但是 id 是数据库生成的,不应该使用持久化唯一值。我在我的类别管理控制器中添加了 permit_params。
类别管理控制器
ActiveAdmin.register Category do
permit_params :id, :name
end
不知道怎么指定id为主键,应该是数据库生成的。当我使用正常的持久化方式时它工作得很好,这是我的常规类别控制器
class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]
before_action :category_params, :only [:create, :new]
def index
@categories = Category.all
end
def show
@title = @category.name;
@posts = @category.posts;
end
private
# Use callbacks to share common setup or constraints between actions.
def set_category
@category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:name);
end
end
此外,如果表格中有四个类别,则在四次违规后,它会提交第五次,因为不再有任何违规。
【问题讨论】: