# mycategories_controller.rb
def create_from_category
category = Category.find(params[:id])
# Cerca se già esiste per quell'utente
existing = Mycategory.find_by(user: Current.user, category: category)
if existing
redirect_to mycategory_path(existing), alert: "Hai già una copia di questa categoria."
return
end
mycategory = Mycategory.new(
name: category.name.parameterize(separator: "_"),
description: category.description,
category: category,
user: Current.user
)
if mycategory.save
redirect_to mycategory_path(mycategory), notice: "Categoria copiata con successo!"
else
redirect_back fallback_location: categories_path, alert: "Errore nella creazione della copia."
end
end