我正在使用一个非常简单的博客应用程序在Rails上学习Ruby,而我刚刚开始使用Active Admin。在添加新类别的同时,我不断获得 activerecord :: recordnotunique in admin :: categoriescontroller#create 。
类别表中已经存在记录,因此存在违规行为。但是ID是生成数据库,如果不使用持续存在唯一值。我已经在我的类别管理器中添加了允许_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
同样,如果表中有四个类别,则在四次违规后提交了第五次,因为没有任何违规行为。
从允许的参数中删除 :id
。
Activerecord为您处理。
ActiveAdmin.register Category do
permit_params :name
end