1 つの製品に対して多くの画像を作成しようとしています。製品ごとの画像の数はユーザーが入力したいだけなので、product と product_image という 2 つの別個のモデルを作成しました。製品には多くの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ラインでdbをチェックしました。
では、特定の製品の ID を使用して作成されるイメージを取得するにはどうすればよいでしょうか? 私はこのサイトを検索しましたが、存在する質問は私が必要とする解決策を提供しているようです. ご協力いただきありがとうございます。
製品と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