我正在尝试为单个产品创建许多图像。 由于每个产品的图像数量与用户想要输入的数量一样多,因此我创建了2个独立的模型,product和product_image。 产品有很多product_images 和product_images属于产品
我几乎可以肯定这段代码是问题(这是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创建图像?我搜索了这个网站,但现有的问题似乎提供了我需要的解决方案。 感谢您提供的任何帮助。
以下是我用于与product和product_images相关的模型的迁移
我为这个烂摊子道歉,我在最初的开发中非常优柔寡断,因为我获得了关于整个轨道系统的更多知识而导致失去了很少的变化
产品
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
答案 0 :(得分:1)
我不确定出了什么问题,因为你的问题信息太少了。但是让我快速展示一下,应该如何设置(简化)。
new rails app:
rails new stack_product
创建模型
rails g model product
rails g model image
你得到了所有这些(你必须在这里手动添加attr_accessible属性)
应用程序/模型/ product.rb
class Product < ActiveRecord::Base
attr_accessible :title, :description
has_many :images
end
app / models / image.rb
class Image < ActiveRecord::Base
attr_accessible :name, :path, :product_id
belongs_to :product, foreign_key: "product_id"
end
分贝/迁移/ 20131011195035_create_products.rb
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.primary_key :id
t.string :title
t.string :description
t.string :image_url
t.timestamps
end
end
end
20131011195421_create_images.rb
class CreateImages < ActiveRecord::Migration
def change
create_table :images do |t|
t.primary_key :id
t.integer :product_id
t.string :name
t.string :path
t.timestamps
end
end
end
在终端中使用rails console。
rails console
火灾:
Product.create({title: 'Ford Mustang', description: 'The one and only Shelby'})
...
Image.create({product_id: 1, name: 'Image Mustang', path: '/images/mustang.png'})
Image.create({product_id: 1, name: 'Image Mustang from behind', path: '/images/mustang2.png'})
然后你可以查询对象
p = Product.find(1)
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", 1]]
=> #<Product id: 1, title: "Ford Mustang", description: "The one and only Shelby", image_url: nil, created_at: "2013-10-11 20:14:06", updated_at: "2013-10-11 20:14:06">
Image.where("product_id=?", p.id)
Image Load (0.3ms) SELECT "images".* FROM "images" WHERE (product_id=1)
=> [#<Image id: 1, product_id: 1, name: "Image Mustang", path: "/images/mustang.png", created_at: "2013-10-11 20:14:09", updated_at: "2013-10-11 20:14:09">, #<Image id: 2, product_id: 1, name: "Image Mustang from behind", path: "/images/mustang2.png", created_at: "2013-10-11 20:14:26", updated_at: "2013-10-11 20:14:26">]
所以这很好用。如果您要为此创建表单,您将拥有一个用于产品的表单和另一个用于图像的表单。图像表单将包含所有产品的下拉列表(productname和value with id)。产品的下拉列表将命名为product_id,然后产品的ID将作为product_id保存在图像表中。
你应该支持这一切,看看Rails是如何完成的。