Infamous Nil Object Error w/ OmniAuth - ruby-on-rails

You can see this error on SO:
Nil object error when using OmniAuth (which normally works)
OmniAuth / Rails - You have a nil object when you didn't expect it
I have the same issue with my OmniAuth strategy. I've written my own, and have tested it both locally on Rails and Sinatra. Both instances return the same error, with Sinatra displaying more helpful information.
"You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]="
It looks like there's some Rack stuff going on behind the scenes that I'm not understanding. Sinatra's last comment in the trace is:
"../lib/rack/utils.rb in set_cookie_header!" at line 194:
case header["Set-Cookie"]
I'm not messing with cookies, though. It seems like this problem goes away randomly for certain people, or doing something simple as clearing the cache has worked for someone. None of those techniques have worked for me. Does anyone know a tried and true way for solving this issue? You can view the repo for the project here: https://github.com/stevenhaddox/oa-casport Thanks!
EDIT: After a lot of thought, I'm guessing OmniAuth doesn't like the fact that my Rack middleware is not configured for SSL. Is there a way to tell OmniAuth to not expect SSL?

It looks like you're forgetting to redirect(callback_path) at the end of the request_phase. Once you add that in I think you'll fall right back in line with the OmniAuth workflow towards getting your auth_hash returned.

Related

factory_girl linter failing because of ActionView::Template::Error

I'm in the process of working out the bugs for updating an app from rails 4.1 to rails 5. When I try to run my tests the factory_girl linter complains that some of the factories are invalid:
/Users/stephen/.rvm/gems/ruby-2.3.1#ssa/gems/factory_girl-4.7.0/lib/factory_girl/linter.rb:13:in `lint!': The following factories are invalid: (FactoryGirl::InvalidFactoryError)
* call_log - Undefined variable: "$green". (ActionView::Template::Error)
* sms_log - Undefined variable: "$green". (ActionView::Template::Error)
etc.
This is all factory girl really gives me for a stack trace. It tells me the where the linter is called but that is it. No indication where something might be going in the view. Is there a way for me to determine this? It seems like this is going to be very difficult to debug fi I don't know where it's coming from.
Having investigated by sitting right next to you, I am going to go out on a limb and say that the model has a callback that sends an email, which triggers the whole ActionView rendering, which apparently has some errors.
More proof that callbacks are bad - particularly ones that send emails.

Capybara + Sorcery helpers

I'm currently working on a project using Sorcery to authenticate users and I have some integration tests done with capybara. When I call current_user or logged_in? sorcery helpers I get this error:
ArgumentError Exception: wrong number of arguments calling `page` (0 for 1)
but when I call them in development mode, it doesn't happen.
After some research I found out that the problem comes from find_by_id method. So if I call Model.find_by_id(1), I get this error. I am very confused because I cannot understand why page method is called and where.
I fixed it by overriding the find_by_id method of my model, but I completely disagree doing this way, so...
Does anybody know what's going on?
Thank you in advance guys
I finally fixed it. The problem was that I included Capybara in my env.rb so my ActiveRecord class were inheriting Capybara methods and find_by_id method for finding elements in a webpage was run instead of ActiveRecord find_by_id.
After deleting "include Capybara" line everything works fine.

NoMethodError undefined method '-#' NoMethodError in Controller Ruby on Rails

Context:
I pulled the most recent code from the repository and tried to make sure that the changes I was about to push up were going to work with that version of the code. This is a Ruby on Rails application. Also worth noting is the fact that when running the main application that I pulled from on the web, this error does not show up. But if I run my branch or the main branch cloned onto my environment, the error always shows up for every url I try. So it is on my end.
Problem:
As soon as I go to localhost:3000, I get the following error:
NoMethodError in HomeController#index
undefined method `-#' for #<ActionDispatch::Response:0x64fd460>
What I've Tried:
I have asked my question on the #rubyonrails IRC channel and nobody was able to determine what was going on through the Full Trace (I haven't posted it here because I wasn't sure what was the best way to do that on here; it didn't look very good in the code block or block quote). I have looked at my HomeController's index method, which is defined as such:
def index
#groups = #current_user.groups
#things = Thing.where(:group_id => #groups.map{|e|e.id})
end
I have also Googled around and haven't found what I need to fix the problem.
What I've Learned So Far:
-# is an operator. Some people may receive a similar error in assuming that Ruby has the shortcut to
variable = variable + 1
that a lot of other languages have:
variable++
Here is an example of that case: Undefined method `+#' for false:FalseClass (NoMethodError) ruby
Question:
Does anyone have any further suggestions on how to find the issue here? Also, if I could easily put the Full Trace on here, formatted in an aesthetically pleasing manner, would someone tell me how? I'm at a loss with this one :(
Update (2/8/2013):
It seems that the issue does not necessarily reside in the HomeController nor home/index.html.erb View. I have attempted to access ANY url with a valid action and the same error occurs with "NoMethodError in..." changing to the corresponding [...]Controller#index.
Update (2/9/2013):
Since this error happens no matter what url I try to navigate to, I decided to look in the routes.rb file in the config folder. I ran my server through rubymine instead of the command line this time, which made it a little easier to read for me. I started looking through all the spit out and I noticed an interested line that consisted of:
["private-key looking thing"] [127.0.0.1] Started GET "/" for 127.0.0.1 at 2013-02-09 18:20:52 -0700
It seems like there is a syntactical error in routes.rb (that's my best guess at this point). This does not explain why this only is an issue on my local environment with the same code sets, but what else do I have to go off of?
Does anyone have any suggested things to be on the look out for while I sift through this file? Not really sure what to be looking for as far as errors are concerned. Rubymines inspection stuff converted all my double quotes to single quotes and doesn't really have anything else to complain about.
Thanks in advance,
Jake Smith
I am guessing it might as well be an syntactical error in the corresponding view page Home/index.html.haml .. I am suspecting there is unintended '-' in front of variable call. I tried to simulate a similar scenario in my rails platform and see following page on browser
undefined method `-#' for false:FalseClass
Correct lines of code
%h1 All Movies
= "filtervalue=#{#isFilterOld}"
= "Sortvalue=#{#isSortOld}"
Edited to simulate the error (observe the - in front of isFilterOld variable)
%h1 All Movies
= "filtervalue=#{-#isFilterOld}"
= "Sortvalue=#{#isSortOld}"
I have fixed the issue!
What fixed it:
Go to the directory where your gems are (for me that was C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1)
Delete all gems except for bundler
Make sure you delete the gems from the /cache/, /gems/, and /specifications/ folders (I just deleted them from the /gems/ folder at first and bundle install indicated that it could still find the gems)
Run bundle install
Further Inquiry:
Does anybody have any idea why this worked? I don't know if at this point I can narrow down which gem was causing the issue because the app is working now (I can visit all the urls with corresponding views). If the issue comes up again, I will delete gems one by one to nail down which one was at least causing the issue for me. But if anyone has any insight on this, a more detailed answer would be greatly appreciated by many more people than just me, I think. Thanks to all who helped thus far!

why OmniAuth::Strategies::Facebook::NoAuthorizationCodeError is not handled in omniauth on_failure callback?

I am using Omniauth for Rails 3.2.3 application.
I have configured the on_failure callback as show below.
OmniAuth.config.on_failure = Proc.new do |env|
UsersController.action(:omniauth_failure).call(env)
end
This handles the error "OmniAuth::Strategies::CallbackError" but not "OmniAuth::Strategies::Facebook::NoAuthorizationCodeError".
How to handle this error?.Surly I can not use rescue_from as the error happens in Rack level.
Any ideas?
Thank you
Ensure that your Facebook Application is not running in "Sandbox Mode"
I've run into the same issue.
By my humble investigation it seems to be a bug in the strategy implemented in the omniauth-facebook gem (and, at a quick glance in several others). This is a nice write-up on exception handling in omniauth. It says that
... OmniAuth strategies [...], if they encounter a problem, call the method fail! and pass in a symbol describing the problem like :invalid_credentials and the exception they encountered. The fail! method ends up calling OmniAuth.config.on_failure and passing in the Rack environment (after doing a few other things like sticking the exception into the environment...
The same can be inferred from an example the original authors kindly provided. In the source it's not emphasized and I haven't found it in the wiki docs, either (but I may have overlooked).
Many strategies, including omniauth-facebook, currently raises the exception which we cannot catch at app level anymore.
#soundar: I wish that it worked this way, as advertised.
#fastcatch: As you pointed out, the strategies are not handling these failure cases correctly.
#Jon Day: I had to patch the Rack App for 'omniauth-facebook' (1.4.0) in order to get the reporting that I needed:
require 'newrelic_rpm'
module OmniAuth
class Builder < ::Rack::Builder
def call_with_error_handling(env)
begin
call_without_error_handling(env)
rescue OmniAuth::Strategies::Facebook::NoAuthorizationCodeError => error
# Do whatever you'd like when rescuing.. I wanted to report to NewRelic.
NewRelic::Agent.notice_error(error, env)
env
end
end
alias_method_chain :call, :error_handling
end
end
I'm not proud of this code, but it is one way to gain control over that exception ;).

What does "Anonymous modules have no name to be referenced by" really mean?

I'm upgrading my Rails app to work with Ruby 1.9 and I keep encountering errors like this:
Anonymous modules have no name to be referenced by
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:585:in `to_constant_name'
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:391:in `qualified_name_for'
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:104:in `rescue in const_missing'
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:94:in `const_missing'
/home/foo/app/config/environment.rb:66:in `block in <top (required)>'
etc.
Google finds all kinds of hits for this, but each of them pertains to a specific fix for one specific gem or app. None of them explain what the message really means.
What is an "anonymous module"?
Where is this error message coming from? (The Ruby interpreter itself?)
What is different about Ruby 1.9 that causes this? (Rails 2.3.8 with Ruby 1.8.7 does not encounter this.)
What is the general/proper way to fix this error?
Line 66 of environment.rb is the configuration for super_exception_notifier (old version, 2.0.8):
ExceptionNotifier.configure_exception_notifier do |config|
config[:sender_address] = %("Foo" <foo#foo.com>)
config[:exception_recipients] = %w(foo#foo.com)
config[:skip_local_notification] = false
end
From what I can tell, ExceptionNotifier is undefined, and ActiveSupport is trying to magically load it, but fails and then fails again trying to print a nice error message.
An anonymous module is a module that is declared like so:
Fred = Module.new do
def meth1
"hello"
end
def meth2
"bye"
end
end
instead of by using the regular Module mod_name <block> syntax. Since they have no module name, you can't retrieve the module name. to_constant_name is attempting to call desc.name.blank? where desc is an anonymous module (with no name).
This error is coming from the ActiveSupport module, which may indicate a bug in the active_support gem or may indicate that some other piece of code is using ActiveSupport incorrectly. The error message alone doesn't give enough information to identify the culprit (to me at least, someone with more rails experience might be able to provide more insight).
Without knowing the offending code it's also hard to say exactly why this error is popping up with 1.9, or what needs to be done to fix it. Considering that there are a lot of un- and under-maintained gems out there that have not been updated for 1.9 yet, I would suspect that ActiveSupport is not the source of the problem. Upgrade all of your gems that have 1.9-compatible versions, and then try disabling your other gems one at a time (if you can) and see if you still get the error.
If you provide a list of the other gems that you are using, someone else who may have encountered the error before may be able to provide some details.
This may happen if you try to exploit ActiveRecord's internal class and module contexts in the wrong way. I had this error yesterday while working on a gem which extends deep inner workings of ActiveRecord. I finally managed to get around this problem by redesigning my code which exploits the inner contexts. It would be interesting to see the surrounding lines of environment.rb:66 for further analysis.
This may happen when the class name doesn't match the filename, in
my case it was a file named application.rb contaning the ApplicationController
class. Renaming the file to application_controller.rb solved the problem.
When I got this error, it was due to a misspelling while defining a class. If you are getting this error, it may be worth examining your module and class definitions for typos.

Resources