Is there a plugin or gem that can help me “invite a friend” on the rails?

I want to add the ability for users to invite a friend.

Email should be generated so that if someone clicks on a link and registers, that person is automatically a friend.

I don’t know what the options are, but I wanted some ideas and strategies as an alternative to creating it from scratch.

+3
source share
4 answers

, ( → → ). , , , ( , ):

# routes.rb
match '/invite/:friend_id' => 'public#invite', :as => :invite

# PublicController
def invite
  session[:referring_friend] = params[:friend_id]
  redirect_to root_path
end

# UsersController
def create
  @user = User.new(params[:user])
  if @user.save
    @user.create_friendship(session[:referring_friend]) if session[:referring_friend]
    ...
  else
    ...
  end
end

, :

class Link < ActiveRecord::Base

  belongs_to :user
  attr_accessible :user, :user_id, :clicks, :conversions

  def click!
    self.class.increment_count(:clicks, self.id)
  end

  def convert!
    self.class.increment_count(:conversions, self.id)
  end

end

# routes.rb
match '/invite/:link_id' => 'links#hit', :as => :invite

# LinksController
def hit
  link = Link.find(params[:link_id])
  link.click!
  session[:referring_link_id] = link.id
  redirect_to root_path # or whatever path (maybe provided by link...)
end

# UsersController
def create
  @user = User.new(params[:user])
  if @user.save
    if session[:referring_link_id]
      link = Link.find(session[:referring_link_id])
      link.convert!
      @user.create_friendship(link.user_id)
    end
    ...
  else
    ...
  end
end

, , .

+2

- , , . , , , "waiting_for_approvment", . , "" "", .

0

:

class User < ActiveRecord::Base
 has_and_belongs_to_many :friends, :class_name => "User", :join_table => "friends_users"
end

, . - :

@current_user.friends << @selected_user

.

0

Source: https://habr.com/ru/post/1783991/


All Articles