Save 36% for Black Friday! Learn more

All threads / Struggling with Active Storage delete/purge on uploads

Ask A Question


You’re not receiving notifications from this thread.

Struggling with Active Storage delete/purge on uploads

spacerobotTR asked in Rails

I have a projects model. A project could have many uploads that could be anything (pdf, word, cad, image etc.) I have the uploading working and I am displaying links to the files on the projects/show page. I want to have a delete button next to the file links to be able to purge them when the link is clicked. I am getting all sorts of routing errors though.

When I click the Remove link_to I am getting the error: No route matches [GET] "/projects/1/delete_upload/2"

Any help or suggestions would be appreciated!

Here is what I have:

Projects Controller:

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]

  # GET /projects
  # GET /projects.json
  def index
    @projects = Project.joins(:proj_status).order("created_at DESC")
    @title = 'Projects - Summary'

  # GET /projects/1
  # GET /projects/1.json
  def show
    @projects = Project.joins(:proj_status).order("created_at DESC")


  def delete_upload
    @attachment = ActiveStorage::Attachment.find(params[:upload_id])
    @attachment.purge # or use purge_later
    redirect_back(fallback_location: projects_path)

  # GET /projects/new
  def new
    @project =

  # GET /projects/1/edit
  def edit

  # POST /projects
  # POST /projects.json
  def create
    @project =
    respond_to do |format|

        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render :show, status: :created, location: @project }
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }

  # PATCH/PUT /projects/1
  # PATCH/PUT /projects/1.json
  def update
    respond_to do |format|
      if @project.update(project_params)
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { render :show, status: :ok, location: @project }
        format.html { render :edit }
        format.json { render json: @project.errors, status: :unprocessable_entity }

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    respond_to do |format|
      format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
      format.json { head :no_content }

    # Use callbacks to share common setup or constraints between actions.
    def set_project
      @project = Project.find(params[:id])

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
        params.require(:project).permit(:projname, :startdate, :enddate,
          :installer, :createdby, :cpdcprojnum, :projmanager, :customername, :customerphone,
          :customeremail, :projtype, :building, :rooms, :description, :status_id, uploads: [])

Projects Show page:

<% @project.uploads.each do |upload| %>
              <li class="list-group-item">
                <div class="media align-items-center">
                  <div class="media-body px-2">
                    <%= link_to upload.filename, rails_blob_path(upload, disposition: :attachment) %> - &nbsp;&nbsp;
                    <%= link_to 'Remove', url_for(controller: :projects, action: :delete_upload, id:, upload_id:, data: { confirm: 'Are you sure?' } %>
                  <a href="javascript:void(0)" class="d-block text-light text-large font-weight-light">&times;</a>
              <% end %>


  resources :projects do
  member do
    delete "delete_upload/:upload_id", action: :delete_upload

No route matches [GET] "/projects/1/delete_upload/2"

You made a GET request to the url, but your routes define a DELETE route.

You must add method: :delete to your link.

So changing the link to this:

url_for(controller: :projects, action: :delete_upload, id:, upload_id:, method: :delete, data: { confirm: 'Are you sure?' }

Adding the method: delete produces this error:

No route matches [GET] "/projects/1/delete_upload/2"

Still made a GET request, so you know that your Rails UJS Javascript is broken if it's not making a DELETE request.

Ahhh gotcha. I'll have to chase that down then. Thanks for the help.

I figured it out. Somehow must have removed the reference to include the application.js file in my application.html.erb. Adding that back in has resolved the issue!

Great! Glad you got it working. 🙌

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 39,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.