开发者

Create a trial period for ruby on rails web app

can anyone tell me the best way to implement a 30 trial period for 开发者_如何学JAVAa ruby on rails web app, much like the way Basecamp from 37signals does?

At the moment I have a user signin up page which then gives a user access to a dashboard displaying current information on their products/pricing etc.

I would like users to be able to signup and have full app functionality which then expires after 30 days.

Thanks


Creating Rails application for x days trial period is pretty much easy.

You want to implement a 30 days trial period for your users then follow:

step 1: create these methods in application_controller.rb like

# application_controller.rb
class ApplicationController < ActionController::Base
 # make expire_on method available for all the controllers
 helper :all
 helper :remaining_days


 # find the remaining trial days for this user
 def remaining_days
  ((current_user.created_at + 30.days).to_date - Date.today).round
 end

 def trial_expired?
  # find current_user who is login. If you are using devise simply current_user will works
  # now that you have remaining_days, check whether trial period is already completed
  if remaining_days <= 0
     redirect_to expires_path
  end
 end
end

Step 2: Display the Remaining Trial Days in application.html.erb

# application.html.erb
<html>
  <head></head>
  <body>
    <span style="color:red"> Your trial period will expire on <%= remaining_days %></span>  Days. Suscribe
  </body>
 </html>

step 3: in every controller write

class YourController < ApplicationController
 before_filter trial_expired?
end

so that if trial is already expired it will not give access to your page, but redirect user back to error message page.

Now create one expires controller and do all the required function there. In expires > index page display some error message like "Your trial period is completed please subscribe................." whatever you like.

This works for me.


Simply store the date when the user account was created, along with a boolean flag whether or not the account has been paid.

Or use a date field in which you set the last day the user has the access to the full functionality (and on registering you set this date to be 30 days from registration).

In any case, read the data upon logging in and display different content based on it.

That would be my solution; but I suppose there might be something better and easier.

Edit I generally agree with what @Roland said in his answer. The boolean flag can be replaced with an information about the level of the account, however for that I would use an integer (0 meaning trial, 1 basic, 2 pro, etc.) over a string tag.

You can also automatically do something to accounts that have not been paid at a given day by scheduling a rake task in cron. By do something to I mean things like: invalidate, delete, change level to basic, etc.


The why I'd approach it is to set a trial_started_at field for the user, and check this every time the user logs in, unless they've purchased.


What Sorrow and Ant said, and but instead of a boolean use a tag for users: Trial, Basic, Pro etc.


As Sorrow suggests:

Use a 'date field in which you set the last day the user has the access to the full functionality >(and on registering you set this date to be 30 days from registration).

I only repost to highlight that this is the better option rather than storing when the trial begins, the code for checking the former is: trial_expiry > Time.now, whereas the code to check the trial started date would have to be more convoluted, and makes it difficult to cater for different length trials.

You can clear the expiry date when they purchase an account, no need to check a seperate field. Some kind of user level is still a good idea, but not necessary to the trial system.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜