user9157191
user9157191

Reputation:

Rails TypeError (no implicit conversion of Symbol into Integer) on a string

After setup a simple token check on the session rails spits out this

TypeError (no implicit conversion of Symbol into Integer)

app/controllers/sessions_controller.rb:21:in `verify_token'

on this method

so, as i checked seems right.

someone has any idea why this is happening on this method?

def verify_token
    @current_user = User.find_by(auth_token: request.headers['token'] [:auth_token])    
    if @current_user    
      render json: {message:"verified",  status: 200}    
    else    
      render_unauthorized("Token failed verification")
    end
end

Upvotes: 1

Views: 2112

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36880

The problem is `request.headers['token'] likely contains a string.

When you do

request.headers['token'] [:auth_token]

You are running the [] method on the string, which method is allowed if it contains an integer.

'carrot'[2]
=> 'r'

But it's not allowed if you use a symbol instead, as it can't automatically convert a symbol into an integer (no implicit conversion)

Probably would be sufficient to do...

@current_user = User.find_by(auth_token: request.headers['token'])

Upvotes: 1

Related Questions