How to implement Carrierwave "Delete downloaded files"?

I am trying to implement Carrierwave Deleting downloaded files . Even though the download is loading correctly, removing the image / logo does not work. As Carrierwave suggested, I will add a checkbox to remove the loaded logo. So I have in my view page:

    <div class="logo">
      <%= f.label :logo %>
      <%= f.file_field :logo, accept: 'image/jpeg,image/gif,image/png', style: "width:100%" %>

      <p>
        <label>
          <%= f.check_box :remove_logo %>
          Remove logo
        </label>
      </p>
    </div>

      

I think by adding this checkbox, now removing the logo should already work. However, checking the box and submitting the form does not remove the logo (it also does not generate an error message). The server says:

Started PATCH "/users/fakename11"
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"βœ“", "authenticity_token"=>"eR3XCiW***Oge0w==", "user"=>{"fullname"=>"Natasha Pacocha", "email"=>"example@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "remove_logo"=>"1", "organization"=>"Bartoletti-Bins", ...etc...}, "commit"=>"Save changes", "id"=>"fakename11"}
Unpermitted parameter: remove_logo

      

I'm not sure what to do with unpermitted parameter

. I tried adding attr_accessor :remove_logo

to the controller as well as the custom model, but it didn't matter.

The gem also speaks of @user.remove_logo!

and @user.save

. So I tried to also add them to the update method in the controller as shown below. But it didn't matter. What am I doing wrong?

  def update
    @user = User.friendly.find(params[:id])
    if @user.update_attributes(update_params)
      if params[:remove_logo]
        @user.remove_logo!
        @user.save
      end
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

def update_params
    params.require(:user).permit(:email,
                                 :fullname,
                                 :website, 
                                 :logo
                                 :password, 
                                 :password_confirmation)
end

      

+3


source to share


1 answer


You just need to add :remove_logo

to update_params

.

Then you can get rid of the following:



if params[:remove_logo]
  @user.remove_logo!
  @user.save
end

      

+14


source







All Articles