こんにちは、コードの何が問題なのかわかりません。私は2つのモデルItemsとImagesとそれらの間の関係を持っています
class Item < ActiveRecord::Base
attr_accessible :category_id, :user_id, :title, :description, :published, :start_date, :end_date, :images_attributes
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images, :reject_if => :all_blank, :allow_destroy => true
mount_uploader :name, ImageUploader
end
class Image < ActiveRecord::Base
belongs_to :item
mount_uploader :image, ImageUploader
attr_accessible :item_id, :name
end
items_controller.rb で Carrierwave の 3 つのインスタンスを呼び出して、作成したアイテムに 3 つの画像を追加します。
def new
@item = Item.new
3.times { @item.images.build }
end
ビュー フォームは次のようになります。
<%= f.fields_for :images do |builder| %>
<p> <%= builder.text_field :name %> </p>
<% end %>
この乱れたコードの結果は次のとおりです。
<input id="item_images_attributes_0_name" name="item[images_attributes][0][name]" type="file">
新しいアイテムを追加して保存すると、データベースにファイル名(suit.jpg) の代わりに オブジェクト データが保存されます。
--- !ruby/object:ActionDispatch::Http::UploadedFile
content_type: image/jpeg
headers: |
Content-Disposition: form-data; name="item[images_attributes][0][name]"; filename="suit.jpg"
Content-Type: image/jpeg
original_filename: suit.jpg
tempfile: !ru
以下のデータベース テーブルのスクリーン ショット:
それを解決する方法を知っている人はいますか?