Rails 更新时删除设计图像

Rails Devise images removed on update

在 rails 中,当您将图像添加为文件字段并尝试更新资源时,如果您不在资源编辑表单中提供新图像,它通常不会更新上次上传的图像,并且图像保持不变。 (我正在使用回形针在 S3 上上传图片)

我已将图像字段添加到用户 table。我正在使用设计来管理注册。当我在编辑页面上使用图像字段时,用户没有添加新图像,因为他已经在创建过程中添加了它,该字段被更新为空,最后一张图像丢失。

我怎样才能防止这种情况发生?

我在用户模型中使用上传器功能:

mount_uploader :backgroundimage, ImageUploader

我的编辑表单包含:

<%= 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 %>

在 application_controller 中,我已将 configure_permitted_paramateres 提供为:

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

如果提供的值为空,您可以删除图像键 参数散列。

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

也可以删除特定类型密钥的密钥。在这种情况下,如果您只想删除图像参数,请同时添加该条件。

params.delete_if { |key, value| value.blank? && [key1, key2].include?(key) }