Carrierwave 将对象数据保存到数据库而不是文件名

发布于 2024-10-20 01:51:11 字数 1738 浏览 6 评论 0原文

你好 我只是无法找出我的代码有什么问题。我有两个模型 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

下面数据库表的屏幕截图:

https://lh3.googleusercontent.com/_USg4QWvHRS0/TXDT0Fn-NUI/AAAAAAAAAHL8/91Qgyp5jK3Q/rierwave-objest-database-saved .jpg

有人知道如何解决它吗?

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 :

https://lh3.googleusercontent.com/_USg4QWvHRS0/TXDT0Fn-NuI/AAAAAAAAHL8/91Qgyp5jK3Q/carrierwave-objest-database-saved.jpg

Is anyone who have an idea how to solve it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-10-27 01:51:11

我有一个类似的问题,我想创建自己的文件名(标识符),所以我在上传器上定义了一个文件名方法(在您的情况下为 ImageUploader)
例如

def filename
  @name ||= "foo"
end

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.

def filename
  @name ||= "foo"
end
网名女生简单气质 2024-10-27 01:51:11

首先,您将上传器安装到名为 :image 的列,但从您的数据库图片来看,您没有具有上述名称的列。

1:为名为 image 的图像创建一个列(“因为这就是您上传的内容。”)

rails g migration add_image_to_images image:string
rake db:migrate

2:更新模型的 attr_accessible 以使用新列。

class Image < ActiveRecord::Base
  belongs_to :item
  mount_uploader :image, ImageUploader  
  attr_accessible :item_id, :name, :image  
end

3:更新您的视图

<%= f.fields_for :images  do |builder| %>
  <p> 
    <%= builder.text_field :name  %>
    <%= builder.file_field :image  %> 
  </p>      
<% end %>

4:从 Item 类中删除未使用的安装。

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
end

我将 :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.")

rails g migration add_image_to_images image:string
rake db:migrate

2: Update your model's attr_accessible to use the new column.

class Image < ActiveRecord::Base
  belongs_to :item
  mount_uploader :image, ImageUploader  
  attr_accessible :item_id, :name, :image  
end

3: Update your view

<%= f.fields_for :images  do |builder| %>
  <p> 
    <%= builder.text_field :name  %>
    <%= builder.file_field :image  %> 
  </p>      
<% end %>

4: remove the unused mount from the Item class.

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
end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文