【发布时间】:2019-12-07 03:42:14
【问题描述】:
我有三个模型:项目、团队和用户。
团队有很多相关的项目。
在我的项目定义中,我想删除与项目关联的团队,但单击删除时没有任何反应。
我的destroy def如下:
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
并在 show.erb.html 中制作按钮以删除项目并链接回项目路径
<%= link_to 'Delete Project', project_path(@project), data: { confirm: 'Are you sure'}, method: :destroy, class:'button is-danger is-outlined' %>
</div>
路线:
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
我对 Rails 还是很陌生,找不到发生这种情况的原因。
编辑 2 - 更多细节
项目控制器如下:
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
【问题讨论】:
-
您是否收到任何类型的错误?您正在尝试删除项目,而不是与其关联的团队,并且您还试图重定向到您刚刚销毁的项目。尝试在 rails 协会文档中查找
dependent: :destroy。 -
试试
@project.destroy!,如果失败会抛出异常。
标签: ruby-on-rails