gpt4 book ai didi

ruby-on-rails - 种子错误 ActiveModel::UnknownAttributeError:列表的未知属性 'excerpt'

转载 作者:行者123 更新时间:2023-11-29 12:51:34 26 4
gpt4 key购买 nike

我正在设置我的种子文件以将一些数据添加到我的 Rails 5 应用程序。当我运行 rake db:seed 时出现此错误

rake aborted!
ActiveModel::UnknownAttributeError: unknown attribute 'excerpt' for List.

无法真正理解为什么我得到这个。我使用 PostgreSQL 作为 DB 和 Rails 5.2。我正在尝试构建一个 API。

我设置了数据库进行了迁移,但种子不起作用。

我还有另外两个模型和一个元素 Controller 。

种子文件:

List.create(title:"West Sweden Road Trip", excerpt:"A cool road trip with stops in harbors of the coast")

我的模型是:

class List < ApplicationRecord
has_many :list_items
has_many :items, through: :list_items
end

Controller :

module Api::V1
class ListsController < ApplicationController
before_action :set_list, only: [:show, :update, :destroy]

# GET /lists
def index
@lists = List.order(:id)

render json: @lists
end

# GET /lists/1
def show
render json: @list
end

# POST /lists
def create
@list = List.new(list_params)

if @list.save
render json: @list, status: :created
else
render json: @list.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /lists/1
def update
if @list.update(list_params)
render json: @list
else
render json: @list.errors, status: :unprocessable_entity
end
end

# DELETE /lists/1
def destroy
@list.destroy
if @list.destroy
head :no_content, status: :ok
else
render json: @list.errors, status: :unprocessable_entity
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_list
@list = List.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def list_params
params.require(:list).permit(:title, :excerpt, :description, :upvotes)
end
end
end

我的架构:

ActiveRecord::Schema.define(version: 2018_10_14_135801) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "items", force: :cascade do |t|
t.integer "type"
t.string "name"
t.text "excerpt"
t.text "description"
t.string "url"
t.integer "upvotes"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "list_items", force: :cascade do |t|
t.bigint "list_id"
t.bigint "item_id"
t.text "description"
t.integer "position"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["item_id"], name: "index_list_items_on_item_id"
t.index ["list_id"], name: "index_list_items_on_list_id"
end

create_table "lists", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_foreign_key "list_items", "items"
add_foreign_key "list_items", "lists"
end

最佳答案

rake aborted! ActiveModel::UnknownAttributeError: unknown attribute 'excerpt' for List.

嗯,List没有名为excerpt的属性,但是Item摘录属性

改成

List.create(title:"West Sweden Road Trip")

更新:

如果要将excerpt 列添加到List,则生成一个迁移

rails g migration add_excerpt_to_lists excerpt:text

然后执行rake db:migrate

关于ruby-on-rails - 种子错误 ActiveModel::UnknownAttributeError:列表的未知属性 'excerpt',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52804149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com