我正在按照本教程here使用paperclip多次上传。 然而,当我添加文件时,我得到了闪光通知,确认画廊已成功添加,但没有上传图像。我确信它是在我的控制器中实现的。
gallery_controller.rb
def update
@gallery = Gallery.friendly.find params[:id]
respond_to do |format|
if @gallery.save
if params[:exhibition_images_attributes]
params[:exhibition_images_attributes].each { |image|
@gallery.exhibition_images.create(image: image)
}
end
format.html { redirect_to @gallery, notice: 'Gallery was successfully updated.' }
format.json { render :show, status: :ok, location: @gallery }
else
format.html { render :edit }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
def edit
@gallery = Gallery.friendly.find params[:id]
@image = @gallery.exhibition_images.new
end
private
def gallery_params
params.require(:gallery).permit(:title, exhibition_images_attributes: [:image])
end
上传表单
<%= bootstrap_form_for(@gallery, :html => {:multipart => true}, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10") do |f| %>
<%= f.text_field :title %>
<%= f.fields_for :exhibition_images do |f| %>
<%= f.file_field "image[]", type: :file, multiple: true %>
<% end %>
<%= f.submit "Create/Update", class: "btn btn-primary" %>
<% end %>
gallery.rb
class Gallery < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
belongs_to :guide
has_many :exhibition_images, :autosave => true
accepts_nested_attributes_for :exhibition_images
end
exhibition_image.rb
class ExhibitionImage < ActiveRecord::Base
belongs_to :gallery, :autosave => true
has_attached_file :image, styles: { small: "100x100", guide: "500x500" }
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end