Ruby on rails Rails模型关系缺少AttributeError
我的应用程序中有一些模型,我正在尝试保存数据,但出现了一个错误: ActiveModel::MissingAttributeError(无法写入未知属性Ruby on rails Rails模型关系缺少AttributeError,ruby-on-rails,ruby,ruby-on-rails-4,model,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 4,Model,Ruby On Rails 3.2,我的应用程序中有一些模型,我正在尝试保存数据,但出现了一个错误: ActiveModel::MissingAttributeError(无法写入未知属性产品\u id): app/controllers/admin\u controller.rb:27:在“创建产品”中 我有三种型号 类别模型 class Category < ActiveRecord::Base has_many :features has_many :products end 类别
产品\u id
):
app/controllers/admin\u controller.rb:27:在“创建产品”中
我有三种型号
类别模型
class Category < ActiveRecord::Base
has_many :features
has_many :products
end
类别
迁移:
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name, null: false
t.boolean :active, null: false, default: false
t.timestamps null: false
end
end
class-CreateCategories
特征模型
class Feature < ActiveRecord::Base
has_and_belongs_to_many :categories
has_and_belongs_to_many :products
end
class功能
迁移
class CreateFeatures < ActiveRecord::Migration
def change
create_table :features do |t|
t.belongs_to :category, index:true
t.string :name, null: false
t.timestamps null: false
end
end
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.belongs_to :category, index:true
t.string :name, null: false
t.text :rating, null: false
t.timestamps null: false
end
end
class CreateFeatures
产品模型
class Product < ActiveRecord::Base
belongs_to :category
has_many :features
end
类产品
迁移
class CreateFeatures < ActiveRecord::Migration
def change
create_table :features do |t|
t.belongs_to :category, index:true
t.string :name, null: false
t.timestamps null: false
end
end
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.belongs_to :category, index:true
t.string :name, null: false
t.text :rating, null: false
t.timestamps null: false
end
end
class CreateProducts
我在尝试保存产品时遇到此错误
ActiveModel::MissingAttributeError(无法写入未知属性产品\u id
):
app/controllers/admin\u controller.rb:27:在“创建产品”中
我不知道发生了什么事
有什么想法吗
谢谢如果你看到你的错误,它会清楚地说 ActiveModel::MissingAttributeError(无法写入未知属性product\u id) 因此,您没有产品id字段,并且查看您的关联和功能表迁移,您忘记在功能表中添加产品id字段。 修正: 要在features表中添加product_id字段,您需要创建新的迁移,然后将其迁移到db:
rails g migration AddProductIdToFeatures product_id:integer
rake db:migrate
由于错误来自admin_控制器,请您也包含该代码好吗?功能是类别和产品型号的联接吗?