0

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
4

1 に答える 1

1

質問の情報が少なすぎるため、何が問題なのかわかりません。しかし、これをどのようにセットアップする必要があるかを簡単に示しましょう(簡略化)。

新しいレールアプリ:

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

アプリ/モデル/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 コンソールを使用します。

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">]

したがって、これは正常に機能します。このためのフォームを作成する場合、製品用に 1 つ、画像用にもう 1 つ作成します。画像フォームには、すべての製品 (製品名と ID 付きの値) のドロップダウンがあります。製品のドロップダウンには product_id という名前が付けられ、製品の ID は image テーブルに product_id として保存されます。

Rails でどのように処理されるかを確認するために、これらすべてを足場にする必要があるかもしれません。

于 2013-10-11T20:38:35.940 に答える