嗨,我只是无法找出我的代码有什么问题。我有两个模型项目和图像以及它们之间的关系
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 %>
这导致了这个 randered 代码:
<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
以下数据库表的屏幕截图:
有谁知道如何解决它?