All threads / Add paywall functionality to app

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Add paywall functionality to app

Stan Smith asked in Rails

I would like to offer people x views of certain pages then prompt them to pay for access.
I was looking ot use this gem acts_as_paywall - https://github.com/BarefootCoders/acts_as_paywall
but wanted to know if there was an easier way to do this.

Thanks?

Hi Stan, if you're looking for something simple, I'd recommend just creating a session and using that session as a counter. The following is a pure Ruby implmentation. You'll want to make some adjustments for your Rails app. I haven't tested it within Rails yet but I think you'll get the point.

# Rails has a sessions object that you can use. the next line creates an object to represent it.
session = {}

# You typically have your content in a database and can pull it out as an object. The following will represent that object.
content = {}

# The following represents the values of your content.
content[:title] = 'Title of Content'
content[:content] = 'Display content'

# Create your pay wall session counter
session[:pay_wall] ||= 1

# Run your conditional to check the counter
if session[:pay_wall] >= 5
    puts 'Add Pay Wall code here'
elsif session[:pay_wall] >= 1
    session[:pay_wall] += 1
    puts content[:content]
else
    session[:pay_wall] = 1
    puts content[:content]
end

# Test the value of your session
puts session[:pay_wall]

Resources you may find interesting on the topic of sessions:
http://www.justinweiss.com/articles/how-rails-sessions-work/
http://api.rubyonrails.org/v5.0.1/classes/ActionDispatch/Integration/Session.html

Thanks John for the thorough answer. I'm going to have a go at rolling this from scratch.

You're welcome! Good luck! :)

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.