Reputation: 249
I have three models: Project, team and users.
Team has many projects associated.
In my projects def I want to delete teams associated to projects but nothing happens when I click delete.
My destroy def is as follows:
def destroy
@project = Project.find(params[:id])
@team = project.team
@project.destroy
# edit #1 shown below:
respond_to do |format|
format.html { redirect_to @team, notice: 'Project was successfully destroyed.' }
end
end
And making the button inside show.erb.html to delete project and link back to project path
<%= link_to 'Delete Project', project_path(@project), data: { confirm: 'Are you sure'}, method: :destroy, class:'button is-danger is-outlined' %>
</div>
Routes:
Rails.application.routes.draw do
resources :projects
resources :teams
as :user do
put '/user/confirmation' => 'confirmations#update', :via => :patch, :as => :update_user_confirmation
end
devise_for :users, controllers: {
registrations: 'registrations',
confirmations: 'confirmations'
}
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
root 'home#index'
end
I'm still very new to rails and cannot find a reason for why this may be occuring.
Projects Controller is as follows:
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, only: [:edit, :update, :destroy]
# GET /projects
# GET /projects.json
def index
@projects = Project.all.order('created_at DESC')
end
# GET /projects/1
# GET /projects/1.json
def show
end
# GET /projects/new
def new
@project = current_user.projects.build
@teams = Team.where('id = ?', current_user.team_id)
end
# GET /projects/1/edit
def edit
@teams = current_user.teams
end
# POST /projects
# POST /projects.json
def create
@project = current_user.projects.build(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# 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 }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project = Project.find(params[:id])
@team = project.team
@project.destroy
# edit #1 shown below:
respond_to do |format|
format.html { redirect_to @team, notice: 'Project was successfully destroyed.' }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name, :description, :team_id)
end
end
Upvotes: 1
Views: 562
Reputation: 130
Redirecting to a resource after you delete it doesn't work, because that resource no longer exists. So the controller action should probably redirect to the deleted projects team:
def destroy
@project = Project.find(params[:id])
@team = project.team
@project.destroy
# edit #1 shown below:
respond_to do |format|
format.html { redirect_to @team, notice: 'Project was successfully destroyed.' }
end
end
Then your link_to
should use method: :delete
, NOT method: :destroy
:
<%= link_to 'Delete Project', project_path(@project), data: { confirm: 'Are you sure'}, method: :delete, class:'button is-danger is-outlined' %>
See if that gets you going, and if not please share some debugging information.
Edit #1:
In your controllers destroy action, you need to do a little more work to handle how rails responds to requests. I didn't notice it missing before but the action should have a respond_to
block where you can choose to respond different ways basically. I modified the above controller action code to give you an example.
Upvotes: 1