我正在尝试为单个产品创建许多图像。由于每个产品的图像数量与用户想要输入的数量一样多,因此我创建了 2 个单独的模型,product 和 product_image。产品有很多 product_images 和 product_images belongs_to product
我几乎可以肯定这部分代码是问题所在(这是 product_image 控制器)
def create
@product_image = ProductImage.new(params[:product_image])
@product = @product_image.product
if @product_image.save
@product_image.product_id = @product.id
@product_image.save
redirect_to @product_image, notice: 'Product image was successfully created.'
else
render :template => "products/edit"
end
end
目前,代码允许我通过回形针上传图像,但完全忽略了 product_id,只是将 product_image_id 放在该字段中。
我通过 cmd 行检查了数据库以查看此内容。
那么如何使用特定产品的 ID 创建图像呢?我已经搜索了这个网站,但存在的问题似乎确实提供了我需要的解决方案。谢谢你尽你所能的帮助。
这是我用于与 products 和 product_images 相关的模型的迁移
我为这个混乱道歉,我在最初的开发中非常优柔寡断,这导致我失去了对整个 Rails 系统的更多了解,因此失去了一些小的改变
产品
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.text :description
t.string :image_url
t.decimal :price, precision: 8, scale: 2
t.timestamps
end
end
end
和,产品
class AddColumnsToProducts < ActiveRecord::Migration
def change
drop_table :products
create_table :products do |t|
t.string :product_title
t.text :product_desc
t.string :product_image_url
t.decimal :price, precision: 8, scale: 2
t.timestamps
end
end
end
和,产品
class AddColumnToProducts < ActiveRecord::Migration
def change
add_column :products, :department, :string
end
end
和,产品
class AddMoreColumnsToProducts < ActiveRecord::Migration
def change
add_column :products, :display_on_home_page, :boolean, default: false
add_column :products, :is_highight_product, :boolean, default: false
end
end
和,产品
class RenameIsHighightProductInProducts < ActiveRecord::Migration
def up
rename_column :products, :is_highight_product, :is_highlight_product
end
def down
end
end
和,产品
class RenameProductImageUrlInProducts < ActiveRecord::Migration
def up
rename_column :products, :product_image_url, :image_url
end
def down
end
end
并创建了产品图片表
class CreateProductImages < ActiveRecord::Migration
def change
create_table :product_images do |t|
t.integer :product_id
t.string :title
t.text :description
t.string :image_file_name
t.string :image_content_type
t.integer :image_file_size
t.datetime :image_updated_at
t.timestamps
end
end
end
和,产品
class AlterTableProducts < ActiveRecord::Migration
def up
end
remove_column :products, :image_url
add_column :products, :product_image_id, :integer
def down
end
end
并且,product_images
class AddColumnToProductImages < ActiveRecord::Migration
def change
add_column :product_images, :image_path, :string
end
end
并且,product_images
class RenameColumnImagePathInProductImages < ActiveRecord::Migration
def up
rename_column :product_images, :image_path, :image_url
end
def down
end
end
并且,product_images
class AddProductTitleColumnToProductImages < ActiveRecord::Migration
def change
add_column :product_images, :product_title, :string
end
end
最后,产品
class DropPriceFromProductsAndAddPriceToProducts < ActiveRecord::Migration
def up
end
remove_column :products, :price
add_column :products, :price, :decimal, :precision => 8, :scale => 2
def down
end
end