开发者

Image file input with Formtastic and ActiveAdmin

I started to use formstatic but I need to make a file field with image preview. I mean, when i edit an object, i want to see the image already lin开发者_运维知识库ked.

How can I do that?

Thank you !


The answer is to use the hint attribute :

ActiveAdmin.register Event do
  form :html => { :enctype => "multipart/form-data" } do |f|
    f.input :map, :as => :file, :hint => f.template.image_tag(f.object.map.url(:thumb))
  end
end

Bye


Use paperclip with formtastic

Formtasitc's github page mentions that it supports paperclip:

:file – a file field. Default for file-attachment attributes matching: paperclip or attachment_fu.

Here are some useful screencasts that will get you going:

Paperclip

Cropping images

EDIT:

To display an image in a column of a grid in ActiveAdmin you need to make a custom column (This is untested and could be flawed, I'm extrapolating this from the documentation):

index do
    column "Title" do |post| 
        link_to image_tag("path to file", :alt => "post image"), admin_post_path(post)
    end
end


Two Gems and one plugin can help your case:

Make sure you look at:

Gems:

Paperclip: https://github.com/thoughtbot/paperclip

RailsCast on PaperClip: http://railscasts.com/episodes/134-paperclip

CarrierWave: https://github.com/carrierwaveuploader/carrierwave

RailsCast on CarrierWave: http://railscasts.com/episodes/253-carrierwave-file-uploads

Jquery File Upload: https://github.com/blueimp/jQuery-File-Upload

Jquery File Upload RailsCast: http://railscasts.com/episodes/381-jquery-file-upload (Need a Pro Account for RailsCast)


As @ianpetzer said, in Rails 4.2 / ActiveAdmin master the current answer causes an object reference to be written out as well. The correct answer for 2016 should be similar to this answer:

form :html => { :multipart => true } do |f|
    f.inputs do
        #...
        f.input :image, required: false, hint: image_tag(object.image.url(:medium)).html_safe
        #...
    end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜