-
Notifications
You must be signed in to change notification settings - Fork 0
Upload file
programrails edited this page Nov 27, 2018
·
1 revision
<% model = (param.respond_to? :object) ? param.object : param %>
<%
uploader_name = model.class.try(:uploaders).try(:keys).try(:first).try(:to_sym) # suppose the model has only one uploader
uploader = model.send(uploader_name)
url = ((uploader._storage.to_s == "CarrierWave::Storage::File") ? request.base_url.to_s : "") + uploader.url.to_s
%>
<div class="field">
<%= label_tag uploader_name.to_s.capitalize + ":"%><br>
<% if uploader.file %>
<%= image_tag url %><br>
<div>Image file name: <%= uploader.file.filename %></div><br><br>
<% end %>
</div>
<% if param.respond_to? :object %>
<% form = param %>
<div class="field">
<%= form.file_field uploader_name %>
</div><br>
<div class="field">
<label>
<%= form.check_box "remove_#{uploader_name}" %>
Remove file
</label>
</div><br><br>
<% end %>
<%# console %>
USAGE EXAMPLE:
How to call this partial:
class User < ApplicationRecord
mount_uploader :avatar, AvatarUploader
end
Show action:
<%#= render 'layouts/uploaded_file', param: @user %>
Edit action:
<%#= render 'layouts/uploaded_file', param: form %>