我正在 Magnific Popup(灯箱)内使用 Carrierwave gem。我想做的是after上传图片,它将显示新上传的图片。
目前,从灯箱上传图像后,它将继续显示您最初查看的图像,并显示“成功”消息,表明您的图像已成功上传。
当前的设置是用户单击照片,将打开一个包含画廊照片的灯箱。在灯箱内,您可以删除当前照片、上传新照片或将其设为您的头像。
照片.js.咖啡:
jQuery ->
$('form#new_photo').fileupload
dataType: "script"
add: (e, data) ->
file = data.files[0]
data.context = $(tmpl("template-upload", file))
$('#new_photo').append(data.context)
data.submit()
progress: (e, data) ->
if data.context
progress = parseInt(data.loaded / data.total * 100, 10)
data.context.find('.bar').css('width', progress + '%')
stop: (e, data) ->
$('.upload').hide()
照片控制器:
def create
@photo = Photo.create(params[:photo])
@photo.user = current_user
if @photo.save
flash[:notice] = "Successfully created photos."
redirect_to :back
else
render :action => 'new'
end
end
显示.html.slim:
-if @user.photos.present?
.slider_container
h4 Photos
a.left_arrow href="#"
img alt="" src="/assets/left_arrow.png" /
ul.slider.parent-container
= hidden_field_tag 'current_index',0
[email protected] /cdn-cgi/l/email-protection_with_index do |photo, index|
li class="#{index > 2 ? 'hide' : ''}"
= link_to image_tag(photo.image_url(:thumb)), "#" + dom_id(photo)
div id="#{dom_id(photo)}" class="mfp-hide"
center
= image_tag(photo.image_url(:popup))
- if @user == current_user
= button_to('Set as Profile Image', [:avatar, photo], class: 'btn')
'
= button_to "remove", photo, :confirm => 'Are you sure?', :method => :delete, class: 'btn'
= form_for Photo.new do |f|
= f.label :image, "Upload photos:"
= f.file_field :image, multiple: true, name: "photo[image]"
script#template-upload type="text/x-tmpl"
.upload
| {%=o.name%}
.progress
.bar style=("width: 0%")