Jakxna360
Jakxna360

Reputation: 885

Need help: Automatically refresh a page (rails)

I have a test application that has a subscription. The subscription expires and I have a controller that handles the renew subscription(I know it's not good practice. I hired a cheap freelancer to do it and I don't really know how to program). The only way I know how to trigger a controller is with a link. I have a variable called 'days_til_expire' that will count down to zero.

Main Question: Is there a way to automatically trigger a refresh that will activate the controller I need. I'm thinking:

if days_till_expire == 0
  page refresh to controller#renew
end

Upvotes: 0

Views: 834

Answers (3)

Richard Peck
Richard Peck

Reputation: 76774

To further @Sergio Tulentsev's answer, which is further to @Wayne Chu's answer (answer-ception?????), this is something called authorization, and can be handled entirely in the backend.

Simple implementation:

#app/models/user.rb
class User < ActiveRecord::Base
   def expired?
      # logic for whether subscription has expired
   end
end

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   before_action :authorized?

   private

   def authorized
      if current_user.expired?
         redirect_to root_path, notice: "Your Subscription has Expired"
      end
   end
end

This will fire every single time your user browses to a controller which inherits from the ApplicationController (BlogsController < ApplicationController).

--

A more integrated approach (instead of having to call a method every single time you browse to a controller) would be to validate against specific objects (using Pundit):

class PostPolicy
  attr_reader :user, :post

  def initialize(user, post)
    @user = user
    @post = post
  end

  def update?
    user.subscribed?
  end
end

The above would be accompanied by a daily process which would cycle through your users, and see if their subscription is valid. If not, it should set a value in the users db, which will then allow you to validate off it.

I've not used Pundit a lot, so the above code would need tweaking a lot. The most basic type of authorization is with roles. The alternative is object-orientated authorization, which will check against the object rather than the user.


If you need "professional help", and if you have a big budget, go to http://anadea.info (I have absolutely no affiliation with them); I worked with them in 2012 and is the reason I've been working with Rails ever since; top quality company.

Don't do it if you don't want to spend $5k+ though, it's not worth it.

enter image description here

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

As an alternative to Wayne's answer:

Whenever user performs a "restricted" action (click to watch a video or whatever it is that your app hides behind the paywall), check subscription status and redirect to renew page if it's expired.

No need to bother with page refreshes. Unless, of course, your app has rich client-side UI (meaning, lots of javascript).

Upvotes: 1

Wayne Chu
Wayne Chu

Reputation: 308

You don't need to trigger the link, you can use polling to check if expired or not, if expired, use js to redirect user.

Upvotes: 0

Related Questions