ruby-on-rails – 在multipart simple_form中更新paperclip头像

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在multipart simple_form中更新paperclip头像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为以下表单创建一个编辑页面.问题是当用户浏览到编辑页面时,品牌名称名称是预先填充的,但即使在“样式”中存在头像时,图像上传字段也显示“没有选择文件”.请让我知道,如果有办法补救这个.谢谢!

我的编辑形式:

  1. <%= simple_form_for @style,:html => { :class => 'form-horizontal' },:remote => true do |m| %>
  2.  
  3. <%= m.input :brand_name,:label => 'Brand',:placeholder => 'Brand' %>
  4. <%= m.input :name,:label => 'Style',:placeholder => 'Style' %>
  5. <%= m.input :avatar,:label => "Image" %>
  6.  
  7. <div class="form-actions" style = "background:none">
  8. <%= m.submit nil,:class => 'btn btn-primary' %>
  9. <%= link_to 'Cancel',styles_path,:class => 'btn' %>
  10. </div>
  11.  
  12. <% end %>

解决方法

昨天刚刚实施.在/ app / inputs中自定义输入
  1. class AvatarInput < SimpleForm::Inputs::FileInput
  2. def input
  3. out = '' # the output string we're going to build
  4. # check if there's an uploaded file (eg: edit mode or form not saved)
  5. if object.send("#{attribute_name}?")
  6. # append preview image to output
  7. # <%= image_tag @user.avatar.url(:thumb),:class => 'thumbnail',id: 'avatar' %>
  8. out << template.image_tag(object.send(attribute_name).url(:thumb),id: 'avatar')
  9. end
  10. # append file input. it will work accordingly with your simple_form wrappers
  11. (out << @builder.file_field(attribute_name,input_html_options)).html_safe
  12. end
  13. end

那你可以做

  1. <%= f.input :avatar,:as => :avatar %>

猜你在找的Ruby相关文章