Skip to content

Instantly share code, notes, and snippets.

@macowie
Created February 24, 2016 17:31
Show Gist options
  • Save macowie/5f5d7483c2ecf2becdba to your computer and use it in GitHub Desktop.
Save macowie/5f5d7483c2ecf2becdba to your computer and use it in GitHub Desktop.
Basic Formtastic Input for working with Paperclip image attachments
class PaperclipImageInput < Formtastic::Inputs::FileInput
def image_html_options
{:class => 'attachment'}.merge(options[:image_html] || {})
end
def to_html
input_wrapping do
label_html <<
image_html <<
builder.file_field(method, input_html_options)
end
end
protected
def image_html
attachment = builder.object.send(method)
if builder.object.new_record? || attachment.nil?
return "".html_safe
end
style = options.fetch(:style, :thumb)
url = attachment.url(style)
builder.template.image_tag(url, image_html_options).html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment