In rails when you add images as file field and you try updating the resource and if you don't provide new images in the resource edit form, it normally doesn't update the last uploaded image and the image remains intact. (I am using paperclip for uploading images on S3)
I have added images fields to the Users table. I am using devise for managing registrations. When I use the image fields on the edit page, and a user doesn't add a new image because he already added it during creation, the field is being updated as empty and the last image is lost.
How can I prevent that from happening?
I am using the uploader function in the user model:
mount_uploader :backgroundimage, ImageUploader
My edit form contains:
<%= bootstrap_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<%= f.email_field :email, autofocus: true, :label => "EMAIL" %>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<%= f.password_field :current_password, autocomplete: "off", :label => "CURRENT PASSWORD (we need your current password to confirm your changes)" %>
<%= f.password_field :password, autocomplete: "off", :label => "NEW PASSWORD (leave blank if you don't want to change it)" %>
<%= f.password_field :password_confirmation, autocomplete: "off", :label => "PASSWORD CONFIRMATION (leave blank if you don't want to change it)" %>
<h1 style = "font-size: 50px; color: #7C064D; margin-bottom: 20px;"><strong>OUR PACKAGES</strong></h1>
<div class="col-md-12 text-center" style = "margin-top: 1em;">
<input class="btn btn-danger" id="hideshow" style="margin-top: 10px; margin-bottom: 10px;" type="button" value="VIEW OUR PACKAGES"></input>
<hr>
</div>
<%= f.text_field :name, autofocus: true, :label => "YOUR COMPANY NAME" %>
<%= f.select :role, User.roles.keys.map{|x| x.upcase}, {:label => "EDIT PACKAGE"} %>
<%= f.text_field :website, autofocus: true, :label => "YOUR WEBSITE" %>
<div class="col-md-12 text-center" style = "margin-top: 1em;">
<input class="btn btn-danger" id="hideshow2" style="margin-top: 10px; margin-bottom: 10px;" type="button" value="EDIT YOUR LANDING PAGE DETAILS"></input>
</div>
<div class = "togglediv2">
<div class = "w3-panel w3-card-2">
<div class = "col-sm-12 col-md-12 col-lg-12">
<%= f.file_field :backgroundimage, :label => "CHOOSE A BACKGROUND IMAGE FOR THE PAGE" , :style => "color: #7C064D;" %>
<%= f.file_field :logoimage, :label => "UPLOAD YOUR LOGO" , :style => "color: #7C064D;" %>
<%= f.text_field :textcolor, :label => "CHOOSE A TEXT COLOR" , :style => "color: #7C064D;" %>
<%= f.text_field :websiteheader, autofocus: true, :label => "ENTER A HEADER FOR THE PAGE" %>
<%= f.text_field :websitesubheader, autofocus: true, :label => "ENTER A SUBHEADER FOR THE PAGE" %>
<%= f.text_area :websitedescription, autofocus: true, :label => "ENTER A DESCRIPTION FOR THE PAGE" %>
</div>
</div>
</div>
<br>
<hr>
<br>
<%= f.text_field :street_address, autofocus: true, :label => "YOUR STREET ADDRESS" %>
<%= f.text_field :city, autofocus: true, :label => "YOUR CITY" %>
<%= f.select :state, options_for_select(us_states), {:label => "YOUR STATE"} %>
<%= f.text_field :zipcode, autofocus: true, :label => "YOUR ZIPCODE" %>
<%= f.text_field :phone_number, autofocus: true, :label => "YOUR PHONE NUMBER" %>
<div class="signin-button">
<%= f.submit "Update", class: "btn btn-danger" %>
</div>
<% end %>
In application_controller, I have provided configure_permitted_paramateres as:
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:email, :password, :website, :password_confirmation, :role, :city, :state, :zipcode, :street_address, :phone_number, :name, :backgroundimage, :logoimage, :websiteheader, :websitesubheader, :websitedescription,:textcolor])
devise_parameter_sanitizer.permit(:account_update, keys: [:email, :password, :website, :password_confirmation, :role, :city, :state, :zipcode, :street_address, :phone_number, :name, :backgroundimage, :logoimage, :websiteheader, :websitesubheader, :websitedescription,:textcolor])
end
You could delete the image keys if the value provided are blank from the parameter hash.
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:email,...])
devise_parameter_sanitizer.permit(:account_update, keys: [:email,..])
# Delete the key value pairs from params hash if value is empty
params.delete_if { |_key, value| value.blank? }
end
The keys can be deleted for a specific type of keys as well. Here in this case if you want to delete only for image parameters, add that condition as well.
params.delete_if { |key, value| value.blank? && [key1, key2].include?(key) }