So in my project Project is parent and Medium is Child. I want to delete one of the childs asynchronously (ajax / js.erb).
When clicking on this link (turbo_method is supposed to imply the asynchronous part of the request but I'm not sure about how it works.) something goes wrong.
<%= link_to "Delete Medium", project_medium_path(medium.project, medium), data: { turbo_method: :delete, turbo_confirm: 'are you sure ?' } %>
Here are my logs.
My problem is that after deleting my media entity, it also deletes my project entity. The fact that deleting my project entity implies a ForeignKeyViolation error is normal, I haven't setup my model correctly yet. Either way, it shouldn't delete my project entity.
Here are my controllers, models and my views disposition :
Projects controller
class ProjectsController < ApplicationController
before_action :set_project, only: %i[ show edit update destroy ]
# GET /projects
def index
@projects = policy_scope(Project).order(date: :desc)
authorize Project
end
# GET /projects/1
def show
@medium = Medium.new(project: @project)
end
# GET /projects/new
def new
@project = Project.new
# Set active user as project owner
@project.user = current_user
authorize @project
end
# GET /projects/1/edit
def edit
end
# POST /projects
def create
@project = Project.new(project_params)
# Set active user as project owner
@project.user = current_user
authorize @project
respond_to do |format|
if @project.save
format.html { redirect_to project_url(@project), notice: "Project was successfully created." }
else
format.html { render :new, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to project_url(@project), notice: "Project was successfully updated." }
else
format.html { render :edit, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url, 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])
authorize @project
end
# Only allow a list of trusted parameters through.
def project_params
params.require(:project).permit(:title, :slug, :subject, :category, :description, :location, :date, :user_id)
end
end
Media controller
class MediaController < ApplicationController
before_action :set_medium, only: %i[ edit update destroy ]
before_action :set_project, only: %i[ create edit update ]
# GET /media
# def index
# end
# GET /media/1
#def show
#end
# GET /media/new
def new
@medium = Medium.new
authorize @medium
end
# GET /media/1/edit
def edit
end
# POST /media
def create
@medium = @project.media.create(medium_params)
authorize @medium
respond_to do |format|
if @medium.save
format.html { redirect_to project_url(@medium.project), notice: "Medium was successfully created." }
format.js
else
format.html { render :new, status: :unprocessable_entity }
format.js
end
end
end
# PATCH/PUT /media/1
def update
respond_to do |format|
if @medium.update(medium_params)
format.html { redirect_to project_url(@medium.project), notice: "Medium was successfully updated." }
else
format.html { render :edit, status: :unprocessable_entity }
end
end
end
# DELETE /media/1
def destroy
@project = @medium.project
@medium.destroy
respond_to do |format|
format.html { redirect_to @project, notice: "Medium was successfully destroyed." }
format.js
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:project_id])
authorize @project
end
# Use callbacks to share common setup or constraints between actions.
def set_medium
@medium = Medium.find(params[:id])
authorize @medium
end
# Only allow a list of trusted parameters through.
def medium_params
params.require(:medium).permit(:title, :description, :author, :location, :date, :priority_index, :project_id, :visual)
end
end
Project model
Medium model
Thanks for the help !



