Reputation: 49
I am contacting you because I have a rather important problem. And that despite my research on stackoverflow or elsewhere, I can not correct after a lot of testing and a lot of tinkering.
I practice by making a site where you can list secrets that I call "gossip".
There is a button that allows you to delete a "gossip".
But when I want to remove the "gossip"
I have an error message which is the following :
From what I understand, it is my relationship between the tables that is causing the problem. I therefore attach you my code of these tables in questions :
class Tag < ApplicationRecord
has_many :gossips, through: :tag_join_gossip, dependent: :destroy
validates :title, presence: true
end
class TagJoinGossip < ApplicationRecord
belongs_to :gossip
belongs_to :tag, optional: true
end
class Gossip < ApplicationRecord
belongs_to :user, optional: true
has_many :likes
has_many :tags, through: :tag_join_gossip
has_many :comments
validates :title, presence: true, length: {minimum: 3, maximum: 14}
validates :content, presence: true, length: {minimum: 1}
end
My controller :
class GossipController < ApplicationController
before_action :authenticate_user
def index
@id = params[:id]
@gossips = Gossip.all
end
def show
@id= params[:id]
@gossip = Gossip.find(params[:id])
@comments = Gossip.find(params[:id]).comments
@comment = Comment.new(content: params['content'], gossip_id: 1, gossip_id: @gossip ,user_id: 1)
end
def new
@gossip = Gossip.new
end
def create
@gossip = Gossip.new(title: params['title'], content: params['content'])
@gossip.user = User.find_by(id: session[:user_id])
if @gossip.save
flash[:success] = "Gossip bien créé!"
redirect_to welcome_index_path
else
flash[:danger] = "Nous n'avons pas réussi à créer le potin pour la (ou les) raison(s) suivante(s) "
render new_gossip_path
end
end
def edit
@gossip = Gossip.find(params[:id])
end
def update
@gossip = Gossip.find(params[:id])
gossip_params = params.require(:gossip).permit(:title, :content)
if @gossip.update(gossip_params)
flash[:success] = "Gossip bien modifié !"
redirect_to welcome_index_path
else
render :edit
flash[:danger] = "Nous n'avons pas réussi à modifier le potin, le titre doit faire 3 lettres minimum et vous devez remplir le contenu."
end
end
def destroy
@gossip = Gossip.find(params[:id])
@gossip.destroy
redirect_to welcome_index_path
end
end
private
def authenticate_user
unless current_user
flash[:danger] = "Veuillez vous connecter."
redirect_to new_session_path
end
end
Maybe there are other files that can be of interest so I put my project's github here : https://github.com/MC4517/Projet-THP-GOSSIP-J1
I've been on it all day, I'm starting to lose my mind lol.
Thank you very much for those who venture into my problem, although I think it must be easy for a large part of you!
Upvotes: 0
Views: 418
Reputation: 7878
You have a record with a foreign key (TagJoinGossip) referencing the record you want to destroy (Gossip). Adding dependent: :destroy to the has_many association should solve it.
Class Gossip < ApplicationRecord
has_many :tags, through: :tag_join_gossip, dependent: :destroy
Upvotes: 1