2012-04-22 11 views
12

Vorrei creare una pagina di modifica per il modulo sottostante. Il problema è che quando l'utente accede alla pagina di modifica, brand_name e nome sono precompilati, ma il campo di caricamento dell'immagine mostra "nessun file scelto" anche quando esiste un avatar per lo "stile". Per favore fatemi sapere se c'è un modo per porvi rimedio. Grazie!Aggiornamento paperclip avatar in multipart simple_form

mia Modifica forma:

<%= 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 %> 

risposta

18

Appena implementato ieri. Fare un ingresso personalizzato in/app/ingressi

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 

allora si può fare

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

Devo inserire la gemma e aggiungerla a simple_form/lib/simple_form/inputs? – Abram

+0

Capito appena messo nella mia app ... input interni ... ottimo lavoro. – Abram

+0

Yay! Sono contento che abbia funzionato. Ricevo la taglia? :) – DVG

0

Il codice per nuove viste/Modifica dal paperclip's github page assomiglia a questo:

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

Così forse si dovrebbe cercare m.file_field e includono :html => { :multipart => true } come bene? Anche se personalmente preferisco lo Attachment-Fu.

+0

Si tratta di simple_form not rails modulo standard –

6

Questo è tutto quello che serviva per far funzionare tutto questo (in Haml):

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