Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Carrierwave's "Removing uploaded files"?

I'm trying to implement Carrierwave's Removing uploaded files. Although uploading itself works correctly, removing the image/logo does not work. As Carrierwave suggested I add a checkbox to my view to remove the uploaded 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, removing the logo is now already supposed to work. However, checking the box and then submitting the form does not remove the logo (it does not generate an error message either). The server says:

Started PATCH "/users/fakename11"
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eR3XCiW***Oge0w==", "user"=>{"fullname"=>"Natasha Pacocha", "email"=>"[email protected]", "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 make of the unpermitted parameter. I've tried adding attr_accessor :remove_logo to the controller as well as the user model, but this made no difference.

The gem also speaks of @user.remove_logo! and @user.save. So I tried also adding them in the update method in the controller, as shown below. But this made no difference. 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
like image 859
Nick Avatar asked Dec 05 '22 21:12

Nick


1 Answers

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
like image 166
Nick Avatar answered Dec 15 '22 00:12

Nick