hyperrjas
hyperrjas

Reputation: 10744

rack-affiliates gem with localhost

I'm messing with Rack::Affiliates but I don't know if it works with the domain localhost in development environment.

1º This is my config in application.rb file:

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

2º I send a email with a link and param aff_id something like: <%= link_to "accept invite", new_user_registration_url(:aff_id => @user.id) %>

3º In root action:

  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

I'm getting the message on console:

We're glad you found us on your own!

What am I doing wrong?

Thanks!

Upvotes: 0

Views: 328

Answers (2)

Awais Tahir
Awais Tahir

Reputation: 178

It's all due to User.find_by_affiliate_tag. have you any column named affiliate_tag. If your are inviting using this link <%= link_to "accept invite", new_user_registration_url(:aff_id => @user.id) %> where you are using @user.id as aff_id.

So you have to use User.find_by_id instead of User.find_by_affiliate_tag

Final code snippet of exmaple contoller will look like

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

Upvotes: 0

oregontrail256
oregontrail256

Reputation: 657

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.

Upvotes: 0

Related Questions