用一个链接进行用户的注册推广:



我的git:   https://github.com/chentianwei411/embeddable_comments

用途:比如推广,拉朋友注册,给推广码,用这个码注册就知道是谁推广的了。

使用这个链接注册的用户,它的user记录中的referred_by_id会储存给它这个链接的用户的id。

也就是说通过ref这个参数,新注册的user和有这个referral_code的user建立了关联。

rails new -m template.rb referral_app

cd referral_app

rails g migration AddReferralsToUsers referral_code referred_by_id:integer referral_completed_at:datetime
  • referral_code:一个转移码
  • referred_by_id: 关联的user的id
  • referral_completed_at: 转移的时间。
rails db:migrate

设置自我关联:

class User < ApplicationRecord

  //belongs_to代表这个user由谁转移的"referred_by who"
belongs_to :referred_by, class_name: "User", optional: true
//has_many代表这个user转移了很多转移用户, 转移用户通过外键referred_by_id关联上这个user
has_many :referred_users, class_name: "User", foreign_key: :referred_by_id before_create :set_referral_code
#validates :referral_code, uniqueness: true
//不需要这条验证,因为set_referral_code会生成不同的字符串 def set_referral_code
loop do
self.referral_code = SecureRandom.hex(6) //生成一个随机的16进制的字符串。12个字符
// 加一个判断,如果这个随机数已经存在于User的记录内,则再次♻️,生成一个随机数并判断
    if self.class.exists?(referral_code: referral_code)
     next
else
break
end
end
end after_create :complete_referral!
//当完成转移后,更新转移的时间。
def complete_referral!
update(referral_completed_at: Time.zone.now)
// 可以增加referred_by user的信用或者发送email,等等其他操作!
end
end

设置ApplicationController

使用这个链接的人都会在他的浏览器的cookies中储存这个参数params[:ref]

class ApplicationController < ActionController::Base
//...略
// 任何行为,都会把url中的参数ref存储在cookies中并保持30天
before_action :set_referral_cookie protected def set_referral_cookie
if params[:ref]
cookies[:referral_code] = {
value: params[:ref],
expires: 30.days.from_now,
}
end
end
end

进入routes.rb, 重写registrationsController中的create方法:

devise_for :users, controllers:{..., registrations: 'users/registrations'}

新建controllers/user/registrations_controller.rb

浏览器输入github.com/plataformatec/devise

然后点击find file按钮。

查找registrations_controller.rb文件。

看create方法,其实只需要修改其中的build_resource()方法。所以:

class Users::RegistrationsController < Devise::RegistrationsController
//打开git,重写create方法,但不需要,只需要重写create中调用的build_resource方法即可
def build_resource(hash = {})
super //调用这个原方法
//然后再加上自己写的代码:
// 如果cookies存在,并且这个cookies的值能够在数据库中的User记录中找到对应的user记录。
if cookies[:referral_code] && referrer = User.find_by(referral_code: cookies[:referral_code])
//resource是一个hash,因此新增一个属性referred_by(关联的对象)。
//新建的user中的referred_by_id属性存储了对应的

(Gorails视频)使用推广链接(params[:ref]),增加注册用户!的更多相关文章

  1. django1.8 增加注册用户其他字段(用户扩展)

    在V1.6及之后版本已经删除get_profile()方法,需要使用userprofile. 1.新建moduel,名为UserProfile: class UserProfile(models.Mo ...

  2. 如何用JS判断推广链接所属的客服

    今天有一个客户提出一个需求:网站有多个在线客服,每个客服都有自己的网站推广链接,当访客通过该客服的推广链接进入网站时,必须指定由该客服接待. 我的实现思路是获取推广链接中特定字符,然后判断字符对应的客 ...

  3. C#的参数修饰符out,params,ref

    using System; namespace ParamsProgram { class TestParams { public static void Main(string[] args)//s ...

  4. c#中的Out, params,ref 细说并沉淀

    1. Out,params,ref之前先记录平时用的最多的按值传递参数的情况,当然默认情况下参数传入函数的默认行为也是按值传递的. 1: //默认情况下参数会按照值传递 2: static int a ...

  5. 增加samba用户提示Failed to add entry for user

    1.首先在Ubuntu安装好samba,具体步骤为:安装samba:sudo apt-get install samba安装smbclient:sudo apt-get install 安装smbfs ...

  6. mysql增加普通用户后无法登陆问题的解决方法

    解决方法: 增加普通用户后,执行: mysql> use mysql mysql> delete from user where user=''; mysql> flush priv ...

  7. mysql 增加删除用户

    mysql 增加用户 (注意:因为MYSQL环境中的命令,所以后面都带一个分号作为命令结束符) 格式:grant select on 数据库.* to 用户名@登录主机 identified by ' ...

  8. (笔记)Mysql命令grant on:增加新用户并控制其权限

    grant on命令用于增加新用户并控制其权限. grant on命令格式:grant select on 数据库.* to 用户名@登录主机 identified by “密码”; 1) 增加一个用 ...

  9. 在Ubuntu中增加root用户登录

    一:增加root用户登录 1.打开终端,输入:sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 2.在弹出的编辑框里输入:gree ...

随机推荐

  1. python --- 25 模块和包

    一.模块 1.导入方式 自己创建的模块名称 切不可和 内置模块的一样 ①  import  模块 ②  import 模块 as  名      设置在此空间的名称 ③  from 模块 import ...

  2. Python3基础 访问在线的有道词典

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. 3545: [ONTAK2010]Peaks 平衡树,最小生成树

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=3545 离线询问,按照权值排个序 就是在克鲁斯卡尔时候维护个treap,到时候挨个查询一下就好 ...

  4. 当图片加载失败时更换图片, Firefox onerror 报错

    当图片加载失败时更换图片. <!DOCTYPE html> <meta charset="UTF-8"> <img src="http:// ...

  5. HDU 5459 Jesus Is Here(递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5459 题意: S(1) = c,S(2) = ff, S(3) = cff,之后S(i) = S(i-1)+S( ...

  6. Js操作Cookie的实现

  7. Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分+前缀

    D. Gadgets for dollars and pounds time limit per test 2 seconds memory limit per test 256 megabytes ...

  8. java用毫秒数做日期计算的一个踩坑记录

    错误示例: Date today = new Date(); Date nextMonth = new Date(today.getTime() + 30* 1000*60*60*24); print ...

  9. mysql 中判断表是否存在 以及表存在则删除

    select * from information_schema.tables where table_name ='student';select * from information_schema ...

  10. centos7安装tomcat8 新手入门 图文教程

    系统环境 操作系统:64位CentOS Linux release 7.2.1511 (Core) JDK版本:1.8.0_121 下载tomcat8压缩包 访问官网:http://tomcat.ap ...