undefined method `downcase' for nil:NilClass - ruby-on-rails-3

describe GameSystem::Client do
def client
GameSystem::Client.new(signature_method: 'HMAC-SHA1', oauth_key: 'kk', oauth_secret: 'bb', oauth_access_token_url: 'https:://localhost')
end
before :each do
WebMock.reset!
end
describe 'response' do
context 'wrong path' do
it 'raises JSON::ParserError on invalid return value' do
stub_request(:get,"https://games.com/game_id/invalid_id").to_return(:status => 200, :body => 'invalid json', :headers => {})
Rails.logger.info "#{client.games_by_id("invalid_id")}"
end
end
end
end
Can anyone let me know what i have been doing wrong or what i could do different ?.
It basically throws this error at the line
#access_token = #consumer.get_access_token(nil) in my client

It'd be good if you could be a bit more specific on what you're trying to do.
The error is pretty self explanatory. You're getting downcase for nil:NilClass because you're trying to call downcase for a nil object...Provide more information and we'll be able to help better.

I realize this question was from way back, but I just ran into this issue in a test suite. Sounded familiar.
My error was
Failure/Error: get :index
ActionView::Template::Error:
undefined method `downcase' for nil:NilClass
As it turns out, i'm using mobile-fu and I was stubbing is_mobile_device? without stubbing also mobile_device. Somewhere along the way, mobile-fu was trying to downcase the device name, which was nil. By adding a stub for the mobile_device, things cleared up. In a before block in the specs, I added:
controller.stub(:is_mobile_device? => true, :mobile_device => 'iphone')
If you're not using mobile-fu, you probably have a different issue. I was able to track the actual issue down with pry and pry-debugger. Definitely worthwhile tools to look into.

Related

Getting ActiveModel::Callbacks to work with ActiveResource

I am trying to get ActiveModel::Callbacks to work with ActiveResource (specifically after_initialize) for a Rails 3 app, but I can't seem to get it to work. I don't get any errors, but the callback method is never executed.
Here is a snippet of code
class User < ActiveResource::Base
extend ActiveModel::Callbacks
define_model_callbacks :initialize, :only => :after
after_initialize :update_info
def update_info
puts 'info'
end
end
For some reason, the update_info is never executed. Anyone have any idea how to get this to work?
In case anyone is interested, I re-read the documentation on this, and what I thought was an explanation of how the code worked under the covers, turned out to be a requirement which stated that I needed to override the method I was adding callbacks to:
def initialize(attributes = {}, persisted = false)
run_callbacks :initialize do
super(attributes, persisted)
end
end
This seems incredibly counter-intuitive to me, as it expects you to track down the signature of the existing method, overwrite it, and add the callback functionality. I hope I am missing something here, and simply making a mistake, but I haven't gotten any other solution to work.
Anyways, here is a monkey patch to provide this callback to all AR classes:
module ActiveResource
class Base
extend ActiveModel::Callbacks
define_model_callbacks :initialize, :only => :after
def initialize_with_callback(attributes = {}, persisted = false)
run_callbacks :initialize do
initialize_without_callback(attributes, persisted)
end
end
alias_method_chain :initialize, :callback
end
end

Rendering and re-raising exceptions in Rails controllers

I'm creating an API in Rails and I've run into a situation where I'd like to alert the user that something bad happened by passing them some JSON with an error message. However, I'd also like to re-raise the exception so that Airbrake (formerly Hoptoad) will still catch the error and notify us so that we can look into the problem a bit more.
I'm currently catching the error like so:
begin
if #myobject.update_attributes(:foo => "bar")
render :json => #myobject, :status => :ok
else
render :json => #myobject.errors, :status => :unprocessable_entity
end
rescue Exception => e
render :json => { :errors => { :message => "Uh oh! Something went wrong." } }
raise e
end
The problem is that my client never gets the JSON message since the raise e stops it from rendering and sends it a generic 500 error.
How should I fix this?
[My Solution]
As suggested by Jordan below, I simply call notify_airbrake(ex) in my code any time that I catch the exception. However, I abstracted it slightly by adding the following to my ApplicationController so that I can easily change from Airbrake to something else in the future:
class ApplicationController < ActionController::Base
...
def notify_exception_service(ex)
notify_airbrake(ex)
end
...
end
So, instead of notify_airbrake(ex) I just call notify_exception_service(ex).
From the Airbrake gem documentation:
If you want to log arbitrary things which you've rescued yourself from a controller, you can do something like this:
rescue => ex
notify_airbrake(ex)
flash[:failure] = 'Encryptions could not be rerouted, try again.'
end
The #notify_airbrake call will send the notice over to Airbrake for later analysis. While in your controllers you use the notify_airbrake method, anywhere else in your code, use Airbrake.notify.
You don't have to re-raise the exception in order to log it with in Airbrake.
As I've said on chat, I think you can't becouse of how rails render things.
When you call render, a #_something_render_variable is set, but the page is not directly render, there is still additional call. Raising an exception obviusly block this flow, actually breaking the render of the webpage.
Changing this behaviour is really hard, you must alias the render method and work on it, I had a similar problem.

Debug the create! method inside the controller create method

The rails 3 appis is using the create! method inside the create of a controller.
Sometimes it works, and sometimes it does not.It fails consistently with always the same use case, however i have checked and rechecked and cannot understand why it fails.
The create! method fails silently, there is no indication on the logs of the problem. How can I make the create! methode more verbose?
Code :
class NotificationPaiementsController < ApplicationController
protect_from_forgery :except =>[:create]
skip_before_filter :authorize, :only => [:create]
def create
logger.debug "params is #{params}"
logger.debug "invoice is #{params[:invoice]}"
logger.debug "payment_status is #{params[:payment_status]}"
logger.debug "txn_id is #{params[:txn_id]}"
#notification_paiement = NotificationPaiement.create!(:params => params,
:cart_id => params[:invoice],
:status=> params[:payment_status],
:transaction_id => params[:txn_id])
logger.debug "notification_paiement is #{#notification_paiement}"
render :nothing=>true
end
end
EDIT:
Thx for your answers, it would have been faster to catch exception, but i managed to identify the problem using new and savevia the console. At the save i had an error about UTF-8 encoding : ArgumentError: invalid byte sequence in UTF-8.
Paypal was changing "molière" in "moli\xE8re" and the error was never displayed.
The create! constructor raises an exception if it fails:
Creates an object just like Base.create but calls save! instead of save so an exception is raised if the record is invalid.
So, if you're going to use create!, you should wrap it in exception handling:
begin
#notification_paiement = NotificationPaiement.create!(...
rescue ActiveRecord::RecordInvalid => e
# Deal with your errors.
end
You can temporarily remove the backtrace silencers in config/initializers/backtrace_silencers.rb in case exception info is being swallowed.

respond_with is asking for location on error

I have a pretty standard authenticate method
private
def authenticate_user
#current_user = User.find_by_authentication_token(params[:token])
unless #current_user
error = { :error => "Invalid token." }
respond_with(error, :status => 401 )
end
end
I am calling the API to ensure the authenticate fails.
I get an error stating
ArgumentError (Nil location provided. Can't build URI.):
app/controllers/api/v1/base_controller.rb:13:in `authenticate_user'
What am I doing wrong?
By the specific flavor of your error, I am guessing that "authenticate_user" is called as part of a "create" action.
If that is the case, I believe the answer I provided here will help you as well.
Assuming, however, that this is part of creating an authenticated session, meaning there is no actual location for the newly created "resource", I would supply nil for the response location, as in:
...
respond_with(error, :status => 401, :location => nil)
...
That will make more sense once you have a look at the linked answer. If it still doesn't make sense, I'll be happy to clarify.
I changed respond_with to render and it worked:
render json: { success: false, message: "an error" }, status: 500

Weird error when delivering mail : undefined method `index' for 2011-09-09 22:15:28 +0200:Time

When I deliver emails I get this weird error :
Failure/Error: publication = FactoryGirl.create(:publication, :author => author)
NoMethodError:
undefined method `index' for 2011-09-09 22:15:28 +0200:Time
And the stack trace is not of any help.
Any idea ?
Very simple, you should NOT use :
default :sent_on => Time.now
in your mailer class.
Was tricky though :)
I've got this error when I passed (mistakenly) the :headers parameter to
the mail method. After removing :headers (and using the headers method instead)
it worked.