【问题标题】:Rails Devise images removed on updateRails Devise 图像在更新时被删除
【发布时间】:2018-08-06 14:29:19
【问题描述】:

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

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

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

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

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_pa​​ramateres 为:

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

【问题讨论】:

  • 我认为很多网站都以单独的形式上传图片,可能是出于这个目的。
  • 我认为您可以从 params 哈希中删除 :backgroundimage 并且它不会更新它。试一试!检查该字段是否为空,如果为空,则使用delete(:KEY) 的哈希方法将其删除。然后继续更新过程...

标签: ruby-on-rails image devise image-uploading insert-update


【解决方案1】:

如果提供的值是空白的,您可以删除图像键 参数哈希。

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) }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 2016-01-23
    • 2021-01-11
    相关资源
    最近更新 更多