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?

Reply

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

Reply

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

Reply

You're welcome! Good luck! :)

Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.