簡體   English   中英

如何檢查用戶是否具有CanCanCan訂閱

[英]How to Check if a User has a Subscription with CanCanCan

我正在使用Koudoku gem構建訂閱站點,並嘗試使用Cancancan阻止對我的Lessons內容的授權,直到注冊用戶進行訂閱為止,但是在設置capability.rb文件時遇到問題。 也使用devise。 我想保持“課程:顯示”操作被阻止,直到用戶購買訂閱為止。 我可以鎖定課程,但無法提出為訂閱者解鎖課程的語言。

當時以為這樣可以用,但是沒有骰子。

@subscription.user_id == current_user.id     

要使此功能正常工作,我需要插入什么代碼?

這是我的lessons_controller.rb的摘錄

class LessonsController < ApplicationController
before_action :set_lesson, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!

def show
 @subscriptions = Subscription.all
 authorize! :read, @lesson
end

訂閱模式如下所示:

create_table "subscriptions", force: :cascade do |t|
t.string   "stripe_id"
t.integer  "plan_id"
t.string   "last_four"
t.integer  "coupon_id"
t.string   "card_type"
t.float    "current_price"
t.integer  "user_id"
t.datetime "created_at",    null: false
t.datetime "updated_at",    null: false
end

用戶架構:

create_table "users", force: :cascade do |t|
t.string   "email",                  default: "", null: false
t.string   "encrypted_password",     default: "", null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",          default: 0,  null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at",                          null: false
t.datetime "updated_at",                          null: false
t.string   "first_name"
t.string   "string"
t.string   "last_name"
end

任何幫助將不勝感激! 謝謝!

只是弄清楚了這一點,以防有人想知道。 我將current_user更改為user並添加了用戶||。 = User.new

這是capability.rb代碼:

def initialize(user)
 user ||= User.new

 if Subscription.exists?(:user_id => user.id)
    can :read, Lesson 
 else 
    false
 end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM