LaFrish
LaFrish

Reputation: 33

Delete button destroys but not redirecting in rails

I have a delete button that deletes the project but does not redirect. I am deleting in the edit view so I am unsure if that is the issue. I did check and it is set to DELETE and not GET.

This is a Ruby on Rails app that uses HAML.

Routes:

                          projects GET       /projects(.:format)                                                                 projects#index
                                   POST      /projects(.:format)                                                                 projects#create
                       new_project GET       /projects/new(.:format)                                                             projects#new
                      edit_project GET       /projects/:id/edit(.:format)                                                        projects#edit
                           project PATCH     /projects/:id(.:format)                                                             projects#update
                                   PUT       /projects/:id(.:format)                                                             projects#update
                                   DELETE    /projects/:id(.:format)                                                             projects#destroy

Haml:

%div.actions-group-delete
 .right
   - if can? :destroy, @project
     = link_to project_path(@project), method: :delete, remote: true, data: { confirm: 'Are you sure you want to permanently delete this project?' }, class: "btn btn--primary btn--auto btn--short btn--delete", title: "Delete project" do
       %i.icon.icon-trash

Projects Controller:

def destroy
    @project_id = params[:id]
    project = Project.accessible_by(current_ability).find_by!(id: @project_id)

    authorize! :destroy, @project

    if @project.destroy.update_attributes(id: @project_id)
      flash[:success] = "The Project was successfully deleted."
      redirect_to projects_path
    else
      flash[:error] = "There was an error trying to delete the Project, please try again later."
      redirect_to edit_project_path(@project)
    end
  end

Project Model:

class Project < ActiveRecord::Base
  belongs_to :user
  has_many :project_items, -> { order("code ASC, name ASC") }, dependent: :destroy

  has_many :project_workers, dependent: :destroy
  has_many :workforces, through: :project_workers
  has_many :worked_hours, through: :project_workers

  has_many :project_equipments, dependent: :destroy
  has_many :equipments, through: :project_equipments
  has_many :equipment_hours, through: :project_equipments

  has_many :collaborators, dependent: :destroy
  has_many :used_items, dependent: :destroy
  has_many :reports, dependent: :destroy

  # has_many :items_used, dependent: :destroy, through: :project_items, source: :used_items

  accepts_nested_attributes_for :project_items, allow_destroy: true
  accepts_nested_attributes_for :project_workers, allow_destroy: true
  accepts_nested_attributes_for :project_equipments, allow_destroy: true
  accepts_nested_attributes_for :collaborators

Upvotes: 0

Views: 515

Answers (1)

Nic Nilov
Nic Nilov

Reputation: 5155

Your link_to is set up with remote: true. This means the link is submitted via an ajax call so the redirect happens in the context of that call.

You need to either remove remote: true or create a delete.js.erb view and return the path to redirect to from your delete action. In the view you can then set window.location to this new path.

Upvotes: 1

Related Questions