Skip to content

Instantly share code, notes, and snippets.

@geniuskidkanyi
Created December 19, 2016 12:01
Show Gist options
  • Save geniuskidkanyi/944a9739e45fd63b857b55032bbf3235 to your computer and use it in GitHub Desktop.
Save geniuskidkanyi/944a9739e45fd63b857b55032bbf3235 to your computer and use it in GitHub Desktop.
def current_user
current_user ||= Chatroom.find cookies.signed[:visitor_id] if cookies.signed[:visitor_id]
if current_user
current_user
else
visitor = Visitor.create!(location: "request.remote_ip", ip: "request.remote_ip")
log_in visitor
chatroom = Chatroom.create!(name: visitor.location)
chatroom_user = chatroom.chatroom_users.where(user_id: visitor.id).first_or_create
end
end
this in the actioncable channel
this the error i get from first line
successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Could not execute command from {"command"=>"subscribe", "identifier"=>"{\"channel\":\"ChatroomsChannel\"}"}) [NameError - undefined local variable or method `request' for #<ChatroomsChannel:0x005612f3b7ccd8>
Did you mean? require]: /home/geniuskid/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_controller/metal/cookies.rb:11:in `cookies' | /home/geniuskid/rails/chatty/app/channels/chatrooms_channel.rb:25:in `current_user' | /home/geniuskid/rails/chatty/app/channels/chatrooms_channel.rb:7:in `subscribed' | /home/geniuskid/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actioncable-5.0.0.1/lib/action_cable/channel/base.rb:236:in `block in subscribe_to_channel' | /home/geniuskid/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/callbacks.rb:126:in `call'
@kashif-umair
Copy link

If we want to track a visitor then the simple way of tracking it can be

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :track_visitor

  def track_visitor
    cookies[:visit_count] ||= 0
    cookies[:visit_count] += 1
    # if you want to save the IP address of the visitor then add the following code.
    # I assume that you have a Visitor model in your application
    Visitor.create location: request.remote_ip
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment