Rails: Paperclip & previews?

前端 未结 3 1283
心在旅途
心在旅途 2020-12-07 17:01

You know on some sites when you\'re asked to upload, say, an avatar, you click on the button, select your file, then hit OK, but before you Submit the page

3条回答
  •  时光取名叫无心
    2020-12-07 17:37

    I found the solution posted here useful, you just need to modify it to be only one file (if you are doing single file upload):

    <%= image_tag @upload.image, id:"something_unique"%>
    
    <%= form_for @upload, :html => { :multipart => true } do |f| %> <%= f.file_field :image, id:"something_else_unique" %> <%= f.submit "Add photo" %> <% end %>

    Note: I used a separate model for paperclip, an upload model that has the image attribute. You can add styling to the image preview tag to format the picture size (otherwise it will be the original size).

提交回复
热议问题