plataformatec/simple_form — Issue #427: wiki: update for custom inputs page
Author: apeacox
Created: 2012-02-02 15:15:39+00:00
State: open

Hi there,

I'm landend on the wiki page https://github.com/plataformatec/simple_form/wiki/Adding-custom-input-components because I was looking to solve the same use case using simple_form 2.0.0rc. I ended up with this code:

```
# define a SimpleForm::Inputs::FileInput subclass that should work with a carrierwave image upload
# app/inputs/image_preview_input.rb
class ImagePreviewInput < SimpleForm::Inputs::FileInput
def input
# get this custom attribute from :input_html
size = input_html_options.delete(:preview_size)
res = ''
if object.send("#{attribute_name}?")
res << template.image_tag(object.send(attribute_name).tap {|o| break o.send(size) if size}.send('url'))
end
(res << @builder.file_field(attribute_name, input_html_options)).html_safe
end
end
```

then I use the new input inside the form. In this eexample, I've specified the :preview_size with a custom version of my carrierwave upload.

```
<%= simple_form_for @some_model do |f| %>
<%= f.input :image, :as => :image_preview, :input_html => {:preview_size => :thumb} %>
<% end %>
```

hope this can help others. In case, I can update wiki page ;)

cheers,
A.