简体   繁体   中英

Updating paperclip avatar in multipart simple_form

I would like to create an edit page for the below form. The problem is that when the user browses to the edit page the brand_name and name are pre-filled, but the image upload field shows 'no file chosen' even when an avatar exists for the 'style'. Please let me know if there is some way to remedy this. Thanks!

My Edit Form:

<%= simple_form_for @style, :html => { :class => 'form-horizontal' }, :remote => true do |m| %>

    <%= m.input :brand_name, :label => 'Brand', :placeholder => 'Brand' %>    
    <%= m.input :name, :label => 'Style', :placeholder => 'Style' %>
    <%= m.input :avatar, :label => "Image" %>

    <div class="form-actions" style = "background:none">
      <%= m.submit nil, :class => 'btn btn-primary' %>
      <%= link_to 'Cancel', styles_path, :class => 'btn' %>
    </div>

<% end %>

Just implemented this yesterday. Make a custom input in /app/inputs

class AvatarInput < SimpleForm::Inputs::FileInput
def input
out = '' # the output string we're going to build
# check if there's an uploaded file (eg: edit mode or form not saved)
if object.send("#{attribute_name}?")
  # append preview image to output
  # <%= image_tag @user.avatar.url(:thumb), :class => 'thumbnail', id: 'avatar' %>
  out << template.image_tag(object.send(attribute_name).url(:thumb), :class => 'thumbnail', id: 'avatar')
  end
# append file input. it will work accordingly with your simple_form wrappers
    (out << @builder.file_field(attribute_name, input_html_options)).html_safe
  end
end

Then you can do

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

This is all I needed for this to work (in haml):

=simple_form_for @user, :html => {:multipart => true } do |f|
  =f.file_field :image

The code for new/edit views from paperclip's github page looks like this:

<%= form_for :user, @user, :url => user_path, :html => { :multipart => true } do |form| %>
  <%= form.file_field :avatar %>
<% end %>

So maybe you should try m.file_field and include :html => { :multipart => true } as well? Though I personally prefer Attachment-Fu .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM