百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

refer

> 编程语言
开源

Ruby在铁路应用上的转接代码

311 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Ruby在铁路应用上的转接代码

Refer

Referral codes and affiliate links for Ruby on Rails applications.

Installation

Add this line to your application's Gemfile:

ruby
gem "refer"

And then execute:

bash
$ bundle

Add Refer to your controllers to store referral cookies:

bash
bin/rails generate refer:install

And add Refer to your model:

bash
bin/rails generate refer:model User
bin/rails db:migrate

‍ Usage

Refer adds a model to your Rails application for tracking referrals and referral codes.

To track referrals, you'll need to

  1. Create a referral code
  2. Set a cookie with the referral code
  3. Create the referral
  4. (Optional) Provide a reward for successful referral
Create a referral code

You can create referral codes through the association:

ruby
user.referral_codes.create #=> randomly generated code
user.referral_codes.create(code: "chris")

To customize the referral code generator:

ruby
Refer.code_generator = ->(referrer) { [referrer.id, SecureRandom.alphanumeric(8)].join("-") }
#=> generates codes like "1-7frb5fUw"

By default, Refer will generate 8 character alphanumeric codes.

Set a referral cookie

To track users, we need to stash the referral code in a cookie when present. By default, Refer will look for ?ref=code and save this in a cookie.

ruby
class ApplicationController  Looks up cookie and attempts referral
      redirect_to root_path
    else
      render :new, status: :unprocessable_entity
    end
  end
end

You can also do this manually:

ruby
Refer.refer(code: "referral_code", referee: user)

Refer will make sure the user has not already been referred and create a Referral.

Check if a user was referred already:
ruby
Refer.referred?(user)
#=> true/false
Accessing Referrals

To access a user's referrals, you can use the referrals association:

ruby
user.referrals #=> [Refer::Referral, Refer::Referral]

This returns a list of Refer::Referral objects.

Accessing Referral

To access a user's referral, you can use the referral association:

ruby
user.referral #=> Refer::Referral

To access a user's referrer, you can use referrer:

ruby
user.referrer #=> User that referred this User

Refer with Devise

To use Refer with Devise, you'll need to customize the Devise controller to track the referral after a successful registration.

We can do this by telling Devise to use a custom controller in the routes and hooking into the create action to track the referral.

ruby
# config/routes.rb
devise_for :users, controllers: { registrations: "users/registrations" }
ruby
# app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
  def create
    super do
      refer resource if resource.persisted?
    end
  end
end

Providing Referral Rewards

There are several common ways of handling rewards for successful referrals:

  • Immediate rewards When the referral is successfully created, you can immediately credit the referrer with their reward.

  • Reward after user actions You can check if a user was referred after they complete the action and provide a reward to the referrer.

  • Time-based rewards To provide a reward X days after a successful referral, you can use a schedule job to check for referrals X days ago and provide rewards to those referrers.

We recommend keeping records for each reward given to a referral so you can limit rewards.

Customizing Models

You can add features to Refer's models by using lazy load hooks.

ruby
# config/initializers/refer.rb
ActiveSupport.on_load :refer_referral do
  # Add features to Refer::Referral model
end

ActiveSupport.on_load :refer_referral_code do
  # Add features to Refer::ReferralCode model
end

ActiveSupport.on_load :refer_visit do
  # Add features to Refer::Visit model
end

Contributing

If you have an issue you'd like to submit, please do so using the issue tracker in GitHub. In order for us to help you in the best way possible, please be as detailed as you can.

License

The gem is available as open source under the terms of the MIT License.

Issues· 12 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言