1

I am running into the following error when testing a Post with HTTParty:

NoMethodError (undefined method `gsub' for nil:NilClass):
  app/models/user.rb:44:in `authorized_domain_referer?'
  app/controllers/api/v1/events_controller.rb:17:in `create'

When I run the following in the rails console:

HTTParty.post("http://localhost:3000/api/v1/events.json", body: {auth_token: User.last.authentication_token}, headers: {"HTTP_REFERER" => "http://one.com"}).parsed_response

It's odd because when I just run User.last.authentication_token, I get the token associated with the user:

User.last.authentication_token
 => "eb7bba8f1fc297a4f16a198b153015217c4ade87610e7aec78a36f0f0c33" 

This is the full output before the error:

  Started POST "/api/v1/events.json" for 127.0.0.1 at 2014-07-11 16:01:53 -0400
Processing by Api::V1::EventsController#create as JSON
  Parameters: {"auth_token"=>"eb7bba8f1fc297a4f16a198b153015217c4ade87610e7aec78a36f0f0c33"}
  MOPED: 127.0.0.1:27017 QUERY        database=trackmetrics_development collection=users selector={"$query"=>{"authentication_token"=>"eb7bba8f1fc297a4f16a198b153015217c4ade87610e7aec78a36f0f0c33"}, "$orderby"=>{:_id=>-1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.7350ms
#<User _id: 53bbdffc5368616e4b020000, name: "elad meidar", email: "elad@shinobidevs.com", encrypted_password: "$2a$10$EaQLyTACVrY6psdKGPeK9ODdSZSOOrZtNsS8r8.553MBoRyGxM8Bi", authentication_token: "eb7bba8f1fc297a4f16a198b153015217c4ade87610e7aec78a36f0f0c33", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 27, current_sign_in_at: 2014-07-11 20:01:45 UTC, last_sign_in_at: 2014-07-11 19:49:03 UTC, current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: 2014-07-08 12:12:07 UTC, confirmation_sent_at: 2014-07-08 12:11:40 UTC, unconfirmed_email: nil>
  MOPED: 127.0.0.1:27017 UPDATE       database=trackmetrics_development collection=users selector={"_id"=>BSON::ObjectId('53bbdffc5368616e4b020000')} update={"$set"=>{"last_sign_in_at"=>2014-07-11 20:01:45 UTC, "current_sign_in_at"=>2014-07-11 20:01:53 UTC, "sign_in_count"=>28}} flags=[]
                         COMMAND      database=trackmetrics_development command={:getlasterror=>1, :w=>1} runtime: 0.5950ms
Completed 500 Internal Server Error in 4ms

NoMethodError (undefined method `gsub' for nil:NilClass):
  app/models/user.rb:44:in `authorized_domain_referer?'
  app/controllers/api/v1/events_controller.rb:17:in `create'


  Rendered /Users/shaunkoo/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.7ms)
  Rendered /Users/shaunkoo/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
  Rendered /Users/shaunkoo/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered /Users/shaunkoo/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (15.5ms)

My authorized_domain_referer? class is the following and it's stored in my user model:

before_create :set_authentication_token

  def authorized_domain_referer?(request_origin)
    encoded_url = URI.encode(request_origin)
    uri = URI.parse(encoded_url)
    domain_name = "#{uri.scheme}://#{uri.host}"
    self.domains.where(url: domain_name).any?
  end

My base_controller is the following:

class Api::BaseController < ApplicationController

  respond_to :json, :xml

  skip_before_filter :verify_authenticity_token

  before_filter :authenticate_from_user_token!
  before_filter :authenticate_user!

  before_filter :cors_preflight_check
  after_filter :set_headers

  def authenticate_from_user_token!
    token = params[:auth_token]
    user = User.where(authentication_token: token).first
    if user
      sign_in user, store: false
    end
  end

Anyone knows why it would be a Nilclass even though the user was properly authenticated and signed in?

Thanks....

ShaunK
  • 1,181
  • 5
  • 22
  • 41
  • Try `URI.encode(request_origin.to_s)` in your `def authorized_domain_referer?` method. [See this.](http://stackoverflow.com/questions/18462667/in-escape-undefined-method-gsub-for-urihttp0x007fa07cb01e08-nomethod) – Fei Oct 15 '14 at 15:56

0 Answers0