andrewculver / koudoku

Robust subscription support for Rails with Stripe.
MIT License
1.16k stars 188 forks source link

How to check if a subscription already exists to protect a page? #131

Open ThomasSysOps opened 9 years ago

ThomasSysOps commented 9 years ago

Hello everyone,

I'm pretty much a beginner in Rails, I have installed Koudoku and tested the payment method, however I'm not sure how to protect a page. I want the page only viewed by who subscribe to our services. How can I do that?

I have tried to setup something like this in my home controller if current_owner and current_owner.subscription.present? but the current_owner is not set.

Best Wishes, Thomas

yas4891 commented 9 years ago

@ThomasSysOps Are you using Devise? If so, you should also use CanCanCan to handle authorization

ThomasSysOps commented 9 years ago

Hey @yas4891, Yes, I'm using Devise. I will check CanCanCan now.

ThomasSysOps commented 9 years ago

Thank you very much I think this will solve my issue, however do you happen to have an example of the ability.rb for Koudoku?

ThomasSysOps commented 9 years ago

For those who also has the same issue my solution was to add this to my controller.

  def page
    if !current_user
        redirect_to "/pricing"
    else 
    if Subscription.exists?(:user_id => current_user.id)
    else 
        redirect_to "/pricing"
    end
  end