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
|
||||
|
@ -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…
Reference in new issue