如何使用Paperclip上传图像时发出通知

时间:2016-06-03 11:41:12

标签: javascript ruby-on-rails paperclip

使用Paperclip上传图片时,有没有办法在“成功上传”等网页上添加通知?

头像上传是我简单表单的一部分,这里是当前代码:

<div class="img-circle">
         <%= profile_avatar_select(@user) %>
    </div>
    <%= f.input :avatar, as: :file, label: "false" %>
      </div>

我的application_helper.rb

def profile_avatar_select(user)  
  return image_tag user.avatar.url(:medium),
                   id: 'image-preview',
                   class: 'img-responsive img-circle profile-image' if user.avatar.exists?
  image_tag 'default-avatar.png', id: 'image-preview',
                                  class: 'img-responsive img-circle profile-image'
end  

并在user.rb中:

has_attached_file :avatar, styles: { medium: '152x152#' }  
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/  
end

1 个答案:

答案 0 :(得分:1)

<强> application.html.erb

<% flash.each do |k,v| %>
    <%= content_tag :div, v, class: "alert alert-#{k} text-center" %>
<% end %>

<强>控制器

#in a method right above render or redirect
flash[:success] = "Image Uploaded"