Home>

I'm trying to build a room chat system using the action cable of Ruby on rails. Because the code was written in imitation, it was difficult to understand and an error occurred.
app/channels/application_cable/connection.rb

module ApplicationCable
  class Connection<ActionCable :: Connection :: Base
    identified_by: current_user
    def connect
      self.current_user = find_verified_user
    end
    private
    def find_verified_user
      verified_user = User.find_by (id: cookies.signed [: user_id])
      if verified_user&&cookies.signed ['user.expires_at']>Time.now
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

Error

User Load (0.6ms) SELECT "users". * FROM "users" WHERE "users". "id" =? LIMIT? [["id", 1], ["LIMIT", 1] ]
  ↳ app/channels/application_cable/connection.rb: 10
There was an exception-NoMethodError (undefined method `>'for nil: NilClass)
cookies.permanent.signed [: user_id])

Does that mean you can't get cookies?
Why can't I get it?
How can cookies be used?

  • Answer # 1

    cookies.signed [: user_id]is the login user ID,
    Assuming thatcookies.signed ['user.expires_at']stores the session expiration,
    First, is the value stored incookies.signed [: user_id]orcookies.signed ['user.expires_at']?
    If you don't save it, you can't even get it ...