【问题标题】:Ruby on Rails paperclip multiple upload with nested attributes带有嵌套属性的 Ruby on Rails 回形针多次上传
【发布时间】:2015-07-20 07:11:50
【问题描述】:

我想知道是否有人可以帮助我上传文件!

我正在尝试使用回形针上传多张图片并具有嵌套属性。

模型

class Trip < ActiveRecord::Base
  has_many :trip_images, :dependent => :destroy
end

class TripImage < ActiveRecord::Base
  belongs_to :trip
  has_attached_file :photo, :styles => { :large => "800x800>", :medium => "500x500>", :thumb => "150x150#" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :photo, content_type: /\Aimage\/.*\Z/
end

控制器

def create
  @trip = Trip.new(trip_params)
  respond_to do |format|
    if @trip.save
      format.html { redirect_to @trip, notice: 'Trip was successfully created.' }
      format.json { render :show, status: :created, location: @trip }
    else
      format.html { render :new }
      format.json { render json: @trip.errors, status: :unprocessable_entity }
    end
  end
end

def trip_params
  params.require(:trip).permit(
    :user_id,
    trip_images_attributes: [:id, :photo])
end

查看

<%= simple_form_for @trip, html: { multipart: true } do |f| %>

  <%= f.simple_fields_for :trip_images do |p| %>
    <%= p.file_field :photo, as: :file, multiple: true %>
  <% end%>

  <%= f.button :submit %>

<% end %>

如何在我的旅行图片数据库中保存多张图片?当我提交表单时,没有任何内容保存到数据库中。

【问题讨论】:

  • 尝试将trip_images_attributes: [:id, :photo])更改为trip_images_attributes: [:id, :photo =&gt; []])
  • 并在Trip模型中添加accepts_nested_attributes_for :trip_images。
  • 您能发布一条请求创建操作的日志吗?
  • @Pavan 当我尝试这个时,我得到一个错误No handler found for [#&lt;ActionDispatch::Http::UploadedFile:0x007f9fdc112d98 @tempfile=#... 我也尝试了@AndreyDeineko 的方法。但它只将 1 条记录保存到数据库中。
  • @AndreyDeineko 我做到了!我可以在有时间的时候提供一些信息.. 可能会将其发布在博客或其他内容中。我还没有看到太多关于 refile 的教程。但是,我无法实现 dropzonejs.. 那东西很头疼!

标签: ruby-on-rails ruby ruby-on-rails-4 paperclip nested-attributes


【解决方案1】:

将:trip_id 添加到trip_images_attributes:

def trip_params
  params.require(:trip).permit(
    :user_id,
    trip_images_attributes: [:trip_id, :id, :photo]) # :_destroy
end

如果您打算删除照片,也可以添加:_destroy。

您还错过了将accepts_nested_attributes_for :trip_images 添加到Trip 模型。

将您的表单更改为以下内容:

= f.simple_fields_for :trip_images do |tp|
  = render 'trip_photos_fields', f: photo
.links
  %br= link_to_add_association 'Add another photo', f

还有_trip_photos_fields.html.haml部分:

  - unless f.object.new_record?
    %br= link_to_remove_association "Delete photo", f
  = link_to image_tag(f.object.photo.url(:medium)), f.object.photo.url, target: '_blank'
  - if f.object.new_record?
    = f.file_field :photo, as: :file

【讨论】:

  • 当我尝试这个时,我检查了我的数据库,并且只有 1 条记录。我试图检索这条记录,这就是我得到的:/images/{"id":1,"trip_id":19,"created_at":"2015-07-20T07:36:17.820Z","updated_at":"2015-07-20T07:36:17.820Z","photo_file_name":null,"photo_content_type":null,"photo_file_size":null,"photo_updated_at":null} 在 img src 标签中,使用这个:&lt;% @trip.trip_images.each do |img| %&gt;&lt;%= image_tag img.to_json %&gt;&lt;% end %&gt; 我试图在这次尝试中保存多张照片
  • @hellomello 所以你原来的问题已经解决了,现在你有另一个问题 - 有 null 而不是 photo_file_name .. 你安装了 imagemagic 吗?为了让回形针工作,这是必要的。
  • 它没有保存我试图完成的多个上传。它只保存了 1 条记录。另外,我通过检查版本来检查是否安装了 ImageMagick,我确实安装了。 Version: ImageMagick 6.8.7-7 Q16 x86_64 2013-11-27 使用 convert -version
  • @hellomello 要让它保存多个上传,您必须更改表单才能真正接受这些照片。我会编辑答案
  • 我一直在查看控制台中的参数,我注意到我在所有图像上都得到了这个:name=\"trip[trip_images_attributes][0][photo][]\";。因此,如果我上传了 5 张图片,则所有 5 张图片都包含在尝试保存到数据库中的参数中。此外,在参数的末尾,有这样的:Unpermitted parameter: photo
猜你喜欢
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多