图片上传预览

心已入冬 提交于 2019-12-01 08:36:01
class WebsiteFormBuilder < ActionView::Helpers::FormBuilder


  def enclosure_image
    file = objectify_options(options)[:object].file_url
    file.blank? ? 'logo-grey.png' : file
  end

end
.nested-fields
  .form-group
    .field.d-inline
      %label.newbtn
        = image_tag f.enclosure_image, :class => "blah"
        = f.file_field :file, :accept => "image/png, image/jpeg, image/gif", :class => "select-file", :onchange => "readURL(this);"
  -#
    = link_to_remove_association "删除", f, :class => "btn btn-danger btn-sm d-inline"
$('.newbtn').bind("click" , function () {
  file = $(this).find(".select-file")[0]
  $(file).click();
});
 
function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
      $(input).siblings('.blah').attr('src', e.target.result);
    };

    reader.readAsDataURL(input.files[0]);
  }
}
.newbtn{
  cursor: pointer;
}
.blah{
  max-width:100px;
  height:100px;
}
 = form_for @website, :builder => WebsiteFormBuilder, :url => { :controller => 'newsites', :action => 'update'} do |f|
      
        .form-group.row
          = f.label "Logo图", :class => "col-sm-2 control-label"
          .col-sm-10
            = f.fields_for :enclosures do |enclosure|
              = render 'enclosure_fields', f: enclosure
            .links
              = link_to_add_association "添加logo", f, :enclosures, :class => "btn btn-info"
      

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!