【问题标题】:Multiple upload of images in rails在rails中多次上传图像
【发布时间】:2016-07-29 04:35:42
【问题描述】:

我是 Ruby on Rails 的新手。我正在创建一个可以将照片存储在图库中的应用程序。我尝试使用回形针上传照片,并且运行成功。但现在我想使用回形针同时在同一个画廊中添加多张照片。我不知道如何实现它。谁能帮帮我?

在 gemfile 中我写了“gem 'paperclip'”。

这是我的模型:

#Gallery Model
class Gallery < ActiveRecord::Base
 has_many :gallery_photos
 validates :name, presence: true
end

#GalleryPhoto Model
class GalleryPhoto < ActiveRecord::Base
 belongs_to :gallery
 has_attached_file :photo, :styles => { :small => "150x150>" },
              :url  => "/assets/gallery_photos/:id/:style/:basename.:extension",
              :path => ":rails_root/public/assets/gallery_photos/:id/:style/:basename.:extension"

 validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png']
end

这是我的 gallery_photo 控制器:

def create

@gallery_photo = GalleryPhoto.new(gallery_photo_params)
respond_to do |format|
  if @gallery_photo.save
    format.html { redirect_to galleries_galleryhome_path(id: @gallery_photo.gallery_id), notice: 'Gallery photo was successfully created.' }
    format.json { render :show, status: :created, location: @gallery_photo }
  else
    format.html { render :new }
    format.json { render json: @gallery_photo.errors, status: :unprocessable_entity }
  end
end
end

private
def gallery_photo_params
  params.require(:gallery_photo).permit(:gallery_id,:photo)
end

这是我的迁移文件:

class AddAttachmentPhotoToGalleryPhotos < ActiveRecord::Migration
 def self.up
  change_table :gallery_photos do |t|
   t.attachment :photo
  end
 end

 def self.down
   remove_attachment :gallery_photos, :photo
 end
end

在我看来,我写了以下行来附加图片:

<%= form_for @gallery_photo,html: { multipart: true},url: gallery_photos_path,method: :post do |f| %>
  <div>
   <%= f.file_field :photo, multiple: true %>
  </div>
  <div class="actions">
   <%= f.submit %>
  </div>
<% end %>

这是 GalleryPhoto 表的字段:

class CreateGalleryPhotos < ActiveRecord::Migration
  def change
    create_table :gallery_photos do |t|
      t.references :gallery, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

附件字段是由我上面提到的迁移文件添加的。

【问题讨论】:

  • 抛出的错误是什么?
  • 它没有存储图像的属性。
  • 可以放表格字段吗?

标签: ruby-on-rails ruby image upload image-gallery


【解决方案1】:

我认为您忘记将附件相关属性添加到 gallery_photos

喜欢

add_column :photos, :photo_file_name,:string
add_column :photos, :photo_content_type,:string
add_column :photos, :photo_file_size,:integer
add_column :photos, :photo_created_at,:datetime

【讨论】:

  • 因为我使用了回形针,所以这个字段会自动添加到表格中。
【解决方案2】:

这行代码失败了

@gallery_photo = GalleryPhoto.new(gallery_photo_params)

因为gallery_photo_params的表单帖子返回的值类似于

{
    gallery_photo: { photo: [file1, file2,...] }
}

如果您想将多张照片更新到图库。您需要更新图库而不是图库照片。

<%= form_for @gallery do |f| %>
  <div>
   <%= f.file_field :gallery_photos, multiple: true %>
  </div>
  <div class="actions">
   <%= f.submit %>
  </div>
<% end %>

在你的画廊控制器中

def update
  if @gallery.update(gallery_params)

    redirect_to some_path
  else
    render :edit
  end
end

如果你想创建一个包含多张照片的画廊也是一样的。

【讨论】:

  • 感谢您提出的解决方案,但是因为我已经使用脚手架创建了两个模型。我想在用户进入画廊主页后上传照片。画廊有一个主页。用户可以通过图库索引页面上的列表导航到该主页。在这个主页上,用户可以从gallery_photo的新页面添加照片,该页面是从gallery主页上的链接导航的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多