Carrierwave 将对象数据保存到数据库而不是文件名
你好 我只是无法找出我的代码有什么问题。我有两个模型 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 %>
这段随机代码的结果是:
添加并保存新项目时,我获取对象数据在我的数据库中保存而不是文件名 (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
下面数据库表的屏幕截图:
有人知道如何解决它吗?
Hi
I just can not find out what is wrong with my code. I have two models Items and Images and relation between them
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
I call the 3 instances of Carrierwave in items_controller.rb to add 3 images to the created item
def new
@item = Item.new
3.times { @item.images.build }
end
The view form looks like this :
<%= f.fields_for :images do |builder| %>
<p> <%= builder.text_field :name %> </p>
<% end %>
Which resoults in this randered code :<input id="item_images_attributes_0_name" name="item[images_attributes][0][name]" type="file">
When Add and save the new item i get object data saved instead of file name ( suit.jpg ) in my database :
--- !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
Screen shot from database table below :
Is anyone who have an idea how to solve it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个类似的问题,我想创建自己的文件名(标识符),所以我在上传器上定义了一个文件名方法(在您的情况下为 ImageUploader)
例如
I had a similar issue, I wanted to create my own filenames (identifiers) so I defined a filename method on the uploader (in your case ImageUploader)
e.g.
首先,您将上传器安装到名为 :image 的列,但从您的数据库图片来看,您没有具有上述名称的列。
1:为名为 image 的图像创建一个列(“因为这就是您上传的内容。”)
2:更新模型的 attr_accessible 以使用新列。
3:更新您的视图
4:从 Item 类中删除未使用的安装。
我将 :name 留在图像上,用作可以添加到图像中的任意值。
另外,通过像您一样抽象您的图像模型,我还假设您想要跟踪图像顺序,因此也许为此添加一个额外的列也是一个好主意。
希望这有帮助。
First things first, Your mounting your uploader to a column named :image but from your pic of your db you dont have a column with said name.
1: Make a column for images called image ("since thats what your uploading.")
2: Update your model's attr_accessible to use the new column.
3: Update your view
4: remove the unused mount from the Item class.
I left :name in place on Image for use as an arbitrary value you can add to your image.
Also by abstracting your image model like you did I would also assume you want to keep track of the image order so maybe an extra column for that would be a good idea too.
Hope this helps.