Created
February 24, 2016 17:31
-
-
Save macowie/5f5d7483c2ecf2becdba to your computer and use it in GitHub Desktop.
Basic Formtastic Input for working with Paperclip image attachments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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