dependent: :destroy is not deleting dependencies from views

Posted by jxdx on Stack Overflow See other posts from Stack Overflow or by jxdx
Published on 2014-06-09T03:15:00Z Indexed on 2014/06/09 3:25 UTC
Read the original article Hit count: 127

Projects have many rooms. When I delete a project from the view, the associated rooms are not deleted. Rooms also have many products which should also be deleted when a project is deleted.

Project class

class Project < ActiveRecord::Base
  belongs_to :user

  has_many :rooms, dependent: :destroy
  has_many :products, through: :rooms
end

Projects Controller

class ProjectsController < ApplicationController
    def destroy
        @project = current_user.projects.find(params[:id])
        if @project.delete
            redirect_to user_projects_path(@project.user)
        end
    end
end

Rooms Controller

class RoomsController < ApplicationController
    def destroy
        @room = Room.find(params[:id])
        if @room.delete
            redirect_to root_path
         end
     end

The delete link in the projects show view.

 = link_to "Delete", project_room_path(room.project, room), method: :delete, data: { confirm: "Are you sure?" }, title: room.title, class: "btn btn-danger"

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about delete