0

我在搞乱Rack::Affiliates但我不知道它是否适用于开发环境中的域 localhost。

1º 这是我在application.rb文件中的配置:

config.middleware.use Rack::Affiliates, {:param => 'aff_id', :ttl => 6.months, :domain => '.localhost'}

2º我发送一封带有链接和参数的电子邮件,aff_id例如: <%= link_to "accept invite", new_user_registration_url(:aff_id => @user.id) %>

3º 在根作用中:

  def index
    if request.env['affiliate.tag'] && affiliate = User.find_by_affiliate_tag(request.env['affiliate.tag'])
      logger.info "Halo, referral! You've been referred here by #{affiliate.name} from #{request.env['affiliate.from']} @ #{Time.at(env['affiliate.time'])}"
    else
      logger.info "We're glad you found us on your own!"
    end
   respond_to do |format|
     format.html
   end
 end

我在控制台上收到消息:

We're glad you found us on your own!

我究竟做错了什么?

谢谢!

4

2 回答 2

0

Did you remember to include config.middleware.use Rack::Affiliates in your config/application.rb file?

If not, add it and see what happens.

Otherwise you can try debugging by changing the if statement to:

if request.env['affiliate.tag']
  logger.info "request.env['affiliate.tag'] = #{request.env['affiliate.tag']}"
else
  logger.info "We're glad you found us on your own!"
end

This should tell you if the affiliate.tag is getting set and if so to what value.

于 2014-05-14T04:17:45.033 回答
0

这都是由于User.find_by_affiliate_tag。你有任何名为affiliate_tag的列吗?如果您使用此链接邀请<%= link_to "accept invite", new_user_registration_url(:aff_id => @user.id) %> 使用@user.idaff_id 。

所以你必须使用User.find_by_id而不是 User.find_by_affiliate_tag

示例控制器的最终代码片段将如下所示

class ExampleController < ApplicationController
  def index
    str = if request.env['affiliate.tag'] && affiliate = User.find_by_id(request.env['affiliate.tag'])
      "Halo, referral! You've been referred here by #{affiliate.name} from #{request.env['affiliate.from']} @ #{Time.at(env['affiliate.time'])}"
    else
      "We're glad you found us on your own!"
    end

    render :text => str
  end
end
于 2018-02-08T13:11:55.580 回答