收藏和取消收藏

schedule_job
cxt 5 years ago
parent e62d39094a
commit 1adeea7f88

@ -0,0 +1,21 @@
class CollectionsController < ApplicationController
before_action :require_login
def create
tip_exception("勿重复收藏") if current_user.collections.find_by(create_params).present?
current_user.collections.create!(create_params)
normal_status("收藏成功,您可以在个人主页对应的栏目中查看")
end
def cancel
collection = current_user.collections.find_by!(create_params)
collection.destroy!
normal_status("操作成功")
end
private
def create_params
params.permit(:container_id, :container_type)
end
end

@ -1,3 +1,4 @@
class Collection < ApplicationRecord
belongs_to :user
belongs_to :container, polymorphic: true, optional: true
end

@ -53,6 +53,9 @@ class Shixun < ApplicationRecord
has_many :shixun_service_configs, :dependent => :destroy
has_many :tidings, as: :container, dependent: :destroy
# 收藏
has_many :collections, as: :container, dependent: :destroy
# 实训审核记录
has_many :shixun_reviews, -> {order("shixun_reviews.created_at desc")}, :dependent => :destroy

@ -16,6 +16,9 @@ class Subject < ApplicationRecord
has_many :subject_appointments, dependent: :destroy
# 收藏
has_many :collections, as: :container, dependent: :destroy
has_many :subject_members, ->{ order("subject_members.position asc")}, dependent: :destroy
has_many :users, through: :subject_members
has_many :tidings, as: :container, dependent: :destroy

@ -48,6 +48,8 @@ class User < ApplicationRecord
has_many :course_messages
has_many :courses, foreign_key: 'tea_id', dependent: :destroy
has_many :collections, dependent: :destroy
#试卷
has_many :exercise_banks, :dependent => :destroy
has_many :exercise_users, :dependent => :destroy

@ -306,6 +306,10 @@ Rails.application.routes.draw do
resources :subject_lists
resources :shixun_lists
resources :collections do
delete :cancel, on: :collection
end
resources :shixuns, param: :identifier do
collection do

@ -0,0 +1,6 @@
class UniqIndexOnCollections < ActiveRecord::Migration[5.2]
def change
remove_index :collections, [:container_type, :container_id]
add_index :collections, [:container_type, :container_id], unique: true
end
end
Loading…
Cancel
Save