I'm just learning RoR and have run into my first real bug. Would really appreciate some guidance with it. I am up to the point of creating a user sign in/out feature for a site I am building which will be maintained through sessions. When the sign in form submits it goes to a sessions controller which calls the 'create' method. When it does this it displays this error:
NoMethodError in
SessionsController#create
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.[]
Here's the code for the create method:
33 def create
34 user = User.authenticate(params[:session][:email],
35 params[:session][:password])
36 if user.nil?
37 flash.now[:error] = "Invalid email/password combination."
38 #title = "Sign in"
39 render 'new'
40 else
41 sign_in user
42 redirect_back_or user
43 end
44 end
The application trace says the error is at
app/controllers/sessions_controller.rb:34:in `create'
So the issue seems to be with the authenticate method. This is a class method defined for my user object. What's weird is that I tested the method in rails console and it works fine. Below is the full trace. Again would be very grateful for some help with this. Thanks you.
Trace:
app/controllers/sessions_controller.rb:34:in
create' actionpack (3.0.4)
lib/action_controller/metal/implicit_render.rb:4:in
send_action' actionpack (3.0.4)
lib/abstract_controller/base.rb:150:in
process_action' actionpack (3.0.4)
lib/action_controller/metal/rendering.rb:11:in
process_action' actionpack (3.0.4)
lib/abstract_controller/callbacks.rb:18:in
block in process_action'
activesupport (3.0.4)
lib/active_support/callbacks.rb:435:in
run_4224187041876590211__process_action_3718750575726612430_callbacks'
activesupport (3.0.4)
lib/active_support/callbacks.rb:409:in
_run_process_action_callbacks'
activesupport (3.0.4)
lib/active_support/callbacks.rb:93:in
run_callbacks' actionpack (3.0.4)
lib/abstract_controller/callbacks.rb:17:in
process_action' actionpack (3.0.4)
lib/action_controller/metal/instrumentation.rb:30:in
block in process_action'
activesupport (3.0.4)
lib/active_support/notifications.rb:52:in
block in instrument' activesupport
(3.0.4)
lib/active_support/notifications/instrumenter.rb:21:in
instrument' activesupport (3.0.4)
lib/active_support/notifications.rb:52:in
instrument' actionpack (3.0.4)
lib/action_controller/metal/instrumentation.rb:29:in
process_action' actionpack (3.0.4)
lib/action_controller/metal/rescue.rb:17:in
process_action' actionpack (3.0.4)
lib/abstract_controller/base.rb:119:in
process' actionpack (3.0.4)
lib/abstract_controller/rendering.rb:41:in
process' actionpack (3.0.4)
lib/action_controller/metal.rb:138:in
dispatch' actionpack (3.0.4)
lib/action_controller/metal/rack_delegation.rb:14:in
dispatch' actionpack (3.0.4)
lib/action_controller/metal.rb:178:in
block in action' actionpack (3.0.4)
lib/action_dispatch/routing/route_set.rb:62:in
call' actionpack (3.0.4)
lib/action_dispatch/routing/route_set.rb:62:in
dispatch' actionpack (3.0.4)
lib/action_dispatch/routing/route_set.rb:27:in
call' rack-mount (0.6.14)
lib/rack/mount/route_set.rb:148:in
block in call' rack-mount (0.6.14)
lib/rack/mount/code_generation.rb:93:in
block in recognize' rack-mount
(0.6.14)
lib/rack/mount/code_generation.rb:68:in
optimized_each' rack-mount (0.6.14)
lib/rack/mount/code_generation.rb:92:in
recognize' rack-mount (0.6.14)
lib/rack/mount/route_set.rb:139:in
call' actionpack (3.0.4)
lib/action_dispatch/routing/route_set.rb:492:in
call' actionpack (3.0.4)
lib/action_dispatch/middleware/best_standards_support.rb:17:in
call' actionpack (3.0.4)
lib/action_dispatch/middleware/head.rb:14:in
call' rack (1.2.2)
lib/rack/methodoverride.rb:24:in
call' actionpack (3.0.4)
lib/action_dispatch/middleware/params_parser.rb:21:in
call' actionpack (3.0.4)
lib/action_dispatch/middleware/flash.rb:182:in
call' actionpack (3.0.4)
lib/action_dispatch/middleware/session/abstract_store.rb:149:in
call' actionpack (3.0.4)
lib/action_dispatch/middleware/cookies.rb:302:incall' activerecord (3.0.4)
lib/active_record/query_cache.rb:32:in
block in call' activerecord (3.0.4)
lib/active_record/connection_adapters/abstract/query_cache.rb:28:in
cache' activerecord (3.0.4)
lib/active_record/query_cache.rb:12:in
cache' activerecord (3.0.4)
lib/active_record/query_cache.rb:31:in
call' activerecord (3.0.4)
lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in
call' actionpack (3.0.4)
lib/action_dispatch/middleware/callbacks.rb:46:in
block in call' activesupport (3.0.4)
lib/active_support/callbacks.rb:415:in
_run_call_callbacks' actionpack
(3.0.4)
lib/action_dispatch/middleware/callbacks.rb:44:in
call' rack (1.2.2)
lib/rack/sendfile.rb:107:in call'
actionpack (3.0.4)
lib/action_dispatch/middleware/remote_ip.rb:48:in
call' actionpack (3.0.4)
lib/action_dispatch/middleware/show_exceptions.rb:47:in
call' railties (3.0.4)
lib/rails/rack/logger.rb:13:incall'
rack (1.2.2) lib/rack/runtime.rb:17:in
call' activesupport (3.0.4)
lib/active_support/cache/strategy/local_cache.rb:72:in
call' rack (1.2.2)
lib/rack/lock.rb:11:in block in call'
<internal:prelude>:10:insynchronize'
rack (1.2.2) lib/rack/lock.rb:11:in
call' actionpack (3.0.4)
lib/action_dispatch/middleware/static.rb:30:in
call' railties (3.0.4)
lib/rails/application.rb:168:in call'
railties (3.0.4)
lib/rails/application.rb:77:in
method_missing' railties (3.0.4)
lib/rails/rack/log_tailer.rb:14:in
call' rack (1.2.2)
lib/rack/content_length.rb:13:in
call' rack (1.2.2)
lib/rack/handler/webrick.rb:52:in
service'
/Users/USERNAME_REMOVED/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/httpserver.rb:111:in
service'
/Users/USERNAME_REMOVED/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/httpserver.rb:70:in
run'
/Users/USERNAME_REMOVED/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/server.rb:183:in
block in start_thread'
params[:session] doesn't exist
def create
user = User.authenticate(params[:session][:email],
params[:session][:password]) if params[:session]
...
Instead of declaring method authenticate in class << self :
class << self
def authenticate(email, submitted_password)
user = find_by_email(email)
(user && user.has_password?(submitted_password)) ? user : nil
end
end
Try this: def self.authenticate(email, submitted_password). Be careful, you have to declare it out of class << self
In this case you are declaring a class method authenticate and you should able to call it in your controller, in the create method.
Related
I'm new to Mongo. I've installed Mongodb from this source. I'm using the mongodb for Rails application. The app loads fine after starting the mongodb and rails. However, it fails when I try to login. The same thing happens when I try to perform some operations in rails console.
Here the model code:
def self.authenticate(email, password)
user = find(:first, :conditions => {:email => email})
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
Its failing at if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
Here is the short error:
Database command 'create' failed: {"ok"=>0.0, "errmsg"=>"collection already exists", "code"=>48}
And, the full application error trace:
mongo (1.3.1) lib/mongo/db.rb:506:in command'
mongo (1.3.1) lib/mongo/db.rb:284:increate_collection'
mongoid (2.2.1) lib/mongoid/collections/master.rb:41:in initialize'
mongoid (2.2.1) lib/mongoid/collection.rb:127:innew'
mongoid (2.2.1) lib/mongoid/collection.rb:127:in master'
mongoid (2.2.1) lib/mongoid/collection.rb:60:infind_one'
mongoid (2.2.1) lib/mongoid/contexts/mongo.rb:164:in first'
mongoid (2.2.1) lib/mongoid/criteria.rb:42:inone'
mongoid (2.2.1) lib/mongoid/criterion/inclusion.rb:104:in find'
mongoid (2.2.1) lib/mongoid/finders.rb:83:infind'
actionpack (3.0.3) lib/action_controller/metal/implicit_render.rb:4:in send_action'
actionpack (3.0.3) lib/abstract_controller/base.rb:151:inprocess_action'
actionpack (3.0.3) lib/action_controller/metal/rendering.rb:11:in process_action'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:18:inblock in process_action'
activesupport (3.0.3) lib/active_support/callbacks.rb:435:in _run__902337259577170033__process_action__4476240526817474042__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in_run_process_action_callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in run_callbacks'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:17:inprocess_action'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:30:in block in process_action'
activesupport (3.0.3) lib/active_support/notifications.rb:52:inblock in instrument'
activesupport (3.0.3) lib/active_support/notifications/instrumenter.rb:21:in instrument'
activesupport (3.0.3) lib/active_support/notifications.rb:52:ininstrument'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:29:in process_action'
actionpack (3.0.3) lib/action_controller/metal/rescue.rb:17:inprocess_action'
actionpack (3.0.3) lib/abstract_controller/base.rb:120:in process'
actionpack (3.0.3) lib/abstract_controller/rendering.rb:40:inprocess'
actionpack (3.0.3) lib/action_controller/metal.rb:138:in dispatch'
actionpack (3.0.3) lib/action_controller/metal/rack_delegation.rb:14:indispatch'
actionpack (3.0.3) lib/action_controller/metal.rb:178:in block in action'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:incall'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in dispatch'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:27:incall'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:inblock in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:75:in optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:inrecognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in call'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:492:incall'
mongoid (2.2.1) lib/rack/mongoid/middleware/identity_map.rb:33:in block in call'
mongoid (2.2.1) lib/mongoid.rb:130:inunit_of_work'
mongoid (2.2.1) lib/rack/mongoid/middleware/identity_map.rb:33:in call'
actionpack (3.0.3) lib/action_dispatch/middleware/best_standards_support.rb:17:incall'
actionpack (3.0.3) lib/action_dispatch/middleware/head.rb:14:in call'
rack (1.2.4) lib/rack/methodoverride.rb:24:incall'
actionpack (3.0.3) lib/action_dispatch/middleware/params_parser.rb:21:in call'
actionpack (3.0.3) lib/action_dispatch/middleware/flash.rb:182:incall'
actionpack (3.0.3) lib/action_dispatch/middleware/session/abstract_store.rb:149:in call'
actionpack (3.0.3) lib/action_dispatch/middleware/cookies.rb:295:incall'
actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:46:in block in call'
activesupport (3.0.3) lib/active_support/callbacks.rb:415:in_run_call_callbacks'
actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:44:in call'
rack (1.2.4) lib/rack/sendfile.rb:106:incall'
actionpack (3.0.3) lib/action_dispatch/middleware/remote_ip.rb:48:in call'
actionpack (3.0.3) lib/action_dispatch/middleware/show_exceptions.rb:46:incall'
railties (3.0.3) lib/rails/rack/logger.rb:13:in call'
rack (1.2.4) lib/rack/runtime.rb:17:incall'
activesupport (3.0.3) lib/active_support/cache/strategy/local_cache.rb:72:in call'
rack (1.2.4) lib/rack/lock.rb:11:inblock in call'
rack (1.2.4) lib/rack/lock.rb:11:in synchronize'
rack (1.2.4) lib/rack/lock.rb:11:incall'
actionpack (3.0.3) lib/action_dispatch/middleware/static.rb:30:in call'
railties (3.0.3) lib/rails/application.rb:168:incall'
railties (3.0.3) lib/rails/application.rb:77:in method_missing'
railties (3.0.3) lib/rails/rack/log_tailer.rb:14:incall'
rack (1.2.4) lib/rack/content_length.rb:13:in call'
rack (1.2.4) lib/rack/handler/webrick.rb:52:inservice'
/Users/reonios/.rvm/rubies/ruby-2.1.7/lib/ruby/2.1.0/webrick/httpserver.rb:138:in service'
/Users/reonios/.rvm/rubies/ruby-2.1.7/lib/ruby/2.1.0/webrick/httpserver.rb:94:inrun'
/Users/reonios/.rvm/rubies/ruby-2.1.7/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Any help is highly appreciated.
After checking the code and googling I found that a special character was sent in every request. My request parameters were like this:
{
"utf8"=>"✓",
"authenticity_token"=>"cAcdDOzfBgalF5em+97mTKw9DAjymfmEmG6hc+dDqhQ=",
"email"=>"super_admin_user#test.com",
"password"=>"[FILTERED]",
"commit"=>"Login"
}
Not sure what triggered 'âœ'. But one of the post in google says, there is a bug related to this present in the 2.2 series and that is got fixed with later versions.
So, I found my mongoid version to be 2.2.1 and once I had it upgraded the problem got resolved.
NOTE: Am new to mongoid and gave a solution with minimal knowledge. Please add details if needed.
I am using rails 4.1.
I have setup a custom controller. I have configured the routes with:
resources :chats
I am trying to make a post request to the controller using jQuery. Here is the code:
$.post('/chats/', { session: { advisor_id: $(this).attr('data-id') } }, function (data) {
eval(data);
});
Edit: And here is the code for my controller -
class ChatsController < ApplicationController
def create
#create the new session
#session = Session.create(:advisor_id => session_params[:advisor_id], :session_type => #session.call!, :client_id => current_user.id)
end
private
def session_params
params[:session].permit(:advisor_id, :client_id, :session_type)
end
end
Edit: And here is the code for my model -
class Session < ActiveRecord::Base
enum session_type: [ :chat, :call ]
belongs_to :client, :class_name => "User", :foreign_key => :client_id
belongs_to :advisor, :class_name => "User", :foreign_key => :advisor_id
has_many :instant_messages
def self.obj_name(id)
o = Session.find(id)
"#{o.client.full_name} and #{o.advisor.full_name}"
end
end
However it throws an error which is:
ArgumentError - wrong number of arguments (1 for 0):
activesupport (4.1.1) lib/active_support/callbacks.rb:80:in run_callbacks'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:19:inprocess_action'
actionpack (4.1.1) lib/action_controller/metal/rescue.rb:29:in process_action'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:31:inblock in process_action'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in block in instrument'
activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:ininstrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in instrument'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:30:inprocess_action'
actionpack (4.1.1) lib/action_controller/metal/params_wrapper.rb:250:in process_action'
activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:18:inprocess_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:136:in process'
actionview (4.1.1) lib/action_view/rendering.rb:30:inprocess'
actionpack (4.1.1) lib/action_controller/metal.rb:195:in dispatch'
actionpack (4.1.1) lib/action_controller/metal/rack_delegation.rb:13:indispatch'
actionpack (4.1.1) lib/action_controller/metal.rb:231:in block in action'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:indispatch'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:48:in call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:inblock in call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676:incall'
rack-pjax (0.7.0) lib/rack/pjax.rb:12:in call'
warden (1.2.3) lib/warden/manager.rb:35:inblock in call'
warden (1.2.3) lib/warden/manager.rb:34:in call'
rack (1.5.2) lib/rack/etag.rb:23:incall'
rack (1.5.2) lib/rack/conditionalget.rb:35:in call'
rack (1.5.2) lib/rack/head.rb:11:incall'
remotipart (1.2.1) lib/remotipart/middleware.rb:27:in call'
actionpack (4.1.1) lib/action_dispatch/middleware/params_parser.rb:27:incall'
actionpack (4.1.1) lib/action_dispatch/middleware/flash.rb:254:in call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:incontext'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in call'
actionpack (4.1.1) lib/action_dispatch/middleware/cookies.rb:560:incall'
activerecord (4.1.1) lib/active_record/query_cache.rb:36:in call'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:incall'
activerecord (4.1.1) lib/active_record/migration.rb:380:in call'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:inblock in call'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in run_callbacks'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:incall'
actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:73:in call'
actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:incall'
better_errors (1.1.0) lib/better_errors/middleware.rb:84:in protected_app_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:79:inbetter_errors_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:56:in call'
actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:incall'
actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in call'
railties (4.1.1) lib/rails/rack/logger.rb:38:incall_app'
railties (4.1.1) lib/rails/rack/logger.rb:20:in block in call'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:inblock in tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:intagged'
railties (4.1.1) lib/rails/rack/logger.rb:20:in call'
quiet_assets (1.0.2) lib/quiet_assets.rb:18:incall_with_quiet_assets'
actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in call'
rack (1.5.2) lib/rack/methodoverride.rb:21:incall'
rack (1.5.2) lib/rack/runtime.rb:17:in call'
activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:incall'
rack (1.5.2) lib/rack/lock.rb:17:in call'
actionpack (4.1.1) lib/action_dispatch/middleware/static.rb:64:incall'
rack (1.5.2) lib/rack/sendfile.rb:112:in call'
railties (4.1.1) lib/rails/engine.rb:514:incall'
railties (4.1.1) lib/rails/application.rb:144:in call'
rack (1.5.2) lib/rack/content_length.rb:14:incall'
thin (1.6.2) lib/thin/connection.rb:86:in block in pre_process'
thin (1.6.2) lib/thin/connection.rb:84:inpre_process'
thin (1.6.2) lib/thin/connection.rb:53:in process'
thin (1.6.2) lib/thin/connection.rb:39:inreceive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in run'
thin (1.6.2) lib/thin/backends/base.rb:73:instart'
thin (1.6.2) lib/thin/server.rb:162:in start'
rack (1.5.2) lib/rack/handler/thin.rb:16:inrun'
rack (1.5.2) lib/rack/server.rb:264:in start'
railties (4.1.1) lib/rails/commands/server.rb:69:instart'
railties (4.1.1) lib/rails/commands/commands_tasks.rb:81:in block in server'
railties (4.1.1) lib/rails/commands/commands_tasks.rb:76:inserver'
railties (4.1.1) lib/rails/commands/commands_tasks.rb:40:in run_command!'
railties (4.1.1) lib/rails/commands.rb:17:in'
bin/rails:8:in <top (required)>'
ruby-debug-ide (0.4.23.beta1) lib/ruby-debug-ide.rb:86:indebug_program'
ruby-debug-ide (0.4.23.beta1) bin/rdebug-ide:110:in <top (required)>'
-e:1:in'
Why is it throwing this error? How can I get it to accept the request?
I managed to solve this problem. The issue was that I had an action in the controller called "send". I now know that "send" is a method used in ruby to call other methods.
Here is some more info on the ruby "send" method. What does send() do in Ruby?
I am writing a very simple proxy wrapper for the reddit api so I can make cross domain JSONP requests (reddit does not allow JSONP calls to my knowledge).
I am using the HTTParty gem to make the call out and retrieve the response, which I immediately render out as json.
I am retrieving the subreddit json perfectly fine so far with this as my route:
match "r/:name" => "api#subreddit"
... and this as my controller action:
def subreddit
render :json => HTTParty.get("http://www.reddit.com/r/" + params[:name] + "/.json"), :callback => params[:callback]
end
As an example, the JSON that comes back is similar to this: http://www.reddit.com/r/pics/.json
My problem is when I attempt to grab the comments for a particular thread.
My route is as so:
match "r/:name/comments/:id" => "api#comments"
... and my controller action is this:
def comments
render :json => HTTParty.get("http://www.reddit.com/r/" + params[:name] + "/comments/" + params[:id] + "/.json"), :callback => params[:callback]
end
The JSON that I want back can be seen here: http://www.reddit.com/r/pics/comments/rhzgc/.json
When I call this action, however, I receive the following error message:
MultiJson::DecodeError in ApiController#comments
Nesting of 20 is too deep
The full stack trace follows:
json (1.6.6) lib/json/common.rb:148:in `parse'
json (1.6.6) lib/json/common.rb:148:in `parse'
multi_json (1.2.0) lib/multi_json/engines/json_common.rb:9:in `decode'
multi_json (1.2.0) lib/multi_json.rb:81:in `decode'
httparty (0.8.1) lib/httparty/parser.rb:116:in `json'
httparty (0.8.1) lib/httparty/parser.rb:136:in `parse_supported_format'
httparty (0.8.1) lib/httparty/parser.rb:103:in `parse'
httparty (0.8.1) lib/httparty/parser.rb:66:in `call'
httparty (0.8.1) lib/httparty/request.rb:222:in `parse_response'
httparty (0.8.1) lib/httparty/request.rb:192:in `handle_response'
httparty (0.8.1) lib/httparty/request.rb:75:in `perform'
httparty (0.8.1) lib/httparty.rb:391:in `perform_request'
httparty (0.8.1) lib/httparty.rb:343:in `get'
httparty (0.8.1) lib/httparty.rb:423:in `get'
app/controllers/api_controller.rb:12:in `comments'
actionpack (3.1.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.1.1) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.1.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.1.1) lib/active_support/callbacks.rb:416:in `_run__2872109728488784816__process_action__2261783022290592822__callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
activerecord (3.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.1.1) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.1.1) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.1.1) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.1) lib/action_controller/metal.rb:236:in `block in action'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:29:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:89:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:532:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.6) lib/rack/etag.rb:23:in `call'
rack (1.3.6) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/flash.rb:243:in `call'
rack (1.3.6) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.6) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/cookies.rb:331:in `call'
activerecord (3.1.1) lib/active_record/query_cache.rb:62:in `call'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.1) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:68:in `call'
rack (1.3.6) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.1) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.6) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.6) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.6) lib/rack/lock.rb:15:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.1) lib/rails/engine.rb:456:in `call'
railties (3.1.1) lib/rails/rack/content_length.rb:16:in `call'
railties (3.1.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service'
/Users/emcummings/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/emcummings/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/emcummings/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
What am I doing wrong here? If the JSON is too big to parse (I find that sort of hard to believe!) what can I do?
EDIT:
Looks like dropping back to the standard NET::HTTP lib solved my problem:
uri = URI.parse("http://www.reddit.com/r/pics/comments/rhzgc/.json")
response = Net::HTTP.get_response(uri)
render :json => response.body, :callback => params[:callback]
#OK!
... but I still am curious why I am failing this same call with HTTParty!
Thanks for the help!
It's not that the JSON is too large. It's nested too deeply. HTTParty tries to decode the results that it gets automatically. Following your stack trace, and the HTTParty dependencies, it relies on multi_json which is using the json gem.
Inside of json there is lib/json/pure/parser.rb. The default max depth set is set in there, specifically on line 79. Something in your returned JSON is 20+ levels deep, triggering the exception.
if !opts.key?(:max_nesting) # defaults to 19
#max_nesting = 19
Switched to the OJ JSON parser by adding the following to my Gemfile:
gem 'oj'
and this issue resolved itself.
It looks like this can be worked around by calling to_json with the max_nesting option set.
json = obj.to_json(max_nesting: 50)
I get a very opaque error with the following code:
client = OAuth2::Client.new(
"#{FACEBOOK_CONFIG['application_id']}", "#{FACEBOOK_CONFIG['secret_key']}",
:site => 'https://graph.facebook.com'
)
token = OAuth2::AccessToken.new(client, fb_token)
token.get('/me')
when I execute token.get('/me'), I get the following output:
OAuth2::Error Exception: OAuth2::Error
This code works fine with v0.4.1 but fails with v0.5.0
Here is the stack trace:
OAuth2::Error
Rails.root: /Users/myname/Sites/mysite
Application Trace | Framework Trace | Full Trace
oauth2 (0.5.0) lib/oauth2/client.rb:107:in `request'
oauth2 (0.5.0) lib/oauth2/access_token.rb:98:in `request'
oauth2 (0.5.0) lib/oauth2/access_token.rb:105:in `get'
app/models/facebook_session.rb:430:in `get_fb_me'
app/models/facebook_session.rb:45:in `get_fb_user_id'
app/models/facebook_session.rb:258:in `authenticate'
app/controllers/devise/registrations_controller.rb:42:in `create'
actionpack (3.0.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.3) lib/abstract_controller/base.rb:151:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.3) lib/active_support/callbacks.rb:470:in `_run__664618575269127242__process_action__2116611615142804558__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.3) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.3) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.3) lib/abstract_controller/base.rb:120:in `process'
actionpack (3.0.3) lib/abstract_controller/rendering.rb:40:in `process'
actionpack (3.0.3) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.3) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.3) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:82:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:492:in `call'
warden (0.10.7) lib/warden/manager.rb:35:in `block in call'
warden (0.10.7) lib/warden/manager.rb:34:in `catch'
warden (0.10.7) lib/warden/manager.rb:34:in `call'
haml (3.0.25) lib/sass/plugin/rack.rb:41:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.3) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/cookies.rb:295:in `call'
activerecord (3.0.3) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.3) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.3) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.3) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:353:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.3) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'
actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.3) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'
railties (3.0.3) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.3) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.3) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.3) lib/rack/lock.rb:11:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.3) lib/rails/application.rb:168:in `call'
railties (3.0.3) lib/rails/application.rb:77:in `method_missing'
railties (3.0.3) lib/rails/rack/debugger.rb:21:in `call'
railties (3.0.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.3) lib/rack/content_length.rb:13:in `call'
rack (1.2.3) lib/rack/handler/webrick.rb:52:in `service'
/Users/myname/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/myname/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/myname/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
I've only used Omniauth recently but it looks like your trace is saying you have something like this in your oauth_helper:
def try_access_token
begin
..
rescue OAuth2::AccessDenied
end
end
If that's the case, try changing OAuth2::AccessDenied to OAuth2::Error. Oauth2::Error provides a .code attribute that you can check to determine if it's an access denied response.
4.1 docs: http://rubydoc.info/gems/oauth2/0.4.1/frames
5.0+ docs: http://rubydoc.info/gems/oauth2/0.5.0/frames
I have the same problem.
Looking at the gem code, it's because the Facebook answer is an HTTP code of 400..599.
when 400..599
e = Error.new(response)
raise e if opts[:raise_errors] || options[:raise_errors]
Sadly, the communication between my server to facebook is using HTTPS, therefore, I haven't been able to intercept the problematic request yet.
UPDATE
I got a 400 error from Facebook :
OAuth "Facebook Platform" "invalid_request" "An active access token must be used to query information about the current user."
UPDATE 2
Interesting discussion here : Facebook access_token invalid?
I ended up rolling oauth2 back to 0.4.1 and my FB login and querying works.
I have a REST API that I am trying to access using Rails 3.0.1 that returns:
<Sessions>
<Session>
<Title>This is a sample title</Title>
...Misc
</Session>
<Session>
<Title>Another Title</Title>
...Misc
</Session>
</Sessions>
I have set up my session.rb class defined:
class Session < ActiveResource::Base
self.site = "http://example.com/rest"
end
The url that will return the XML is http://example.com/rest/sessions.
I then have a controller that is defined as this:
class HomeController < ApplicationController
def index
Session.all
end
end
and my view, index.html.erb, is defined:
<h1>List Sessions</h1>
<% #sessions.each do |session| %>
<div><%= session.title %></div>
<% end %>
When I try to view this page, I get the following error:
NoMethodError in HomeController#index
undefined method `collect!' for #<Hash:0x00000105178af8>
What could be causing this issue? I can access the url just fine (even using http://example.com/rest/sessions.xml). Is there a way to debug ActiveResource to see what is happening inside?
EDIT
Here is the stack trace:
activeresource (3.0.1) lib/active_resource/base.rb:885:in `instantiate_collection'
activeresource (3.0.1) lib/active_resource/base.rb:857:in `find_every'
activeresource (3.0.1) lib/active_resource/base.rb:777:in `find'
activeresource (3.0.1) lib/active_resource/base.rb:803:in `all'
app/controllers/home_controller.rb:4:in `index'
actionpack (3.0.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.1) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.1) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.1) lib/active_support/callbacks.rb:435:in `_run__140584718597918073__process_action__4423897468290757938__callbacks'
activesupport (3.0.1) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.1) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.1) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.1) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.1) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.1) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.1) lib/abstract_controller/rendering.rb:40:in `process'
actionpack (3.0.1) lib/action_controller/metal.rb:133:in `dispatch'
actionpack (3.0.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.1) lib/action_controller/metal.rb:173:in `block in action'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:492:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.1) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/cookies.rb:287:in `call'
activerecord (3.0.1) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.1) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.1) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.1) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'
actionpack (3.0.1) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.1) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'
railties (3.0.1) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.1) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.1) lib/rack/lock.rb:11:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.1) lib/rails/application.rb:168:in `call'
railties (3.0.1) lib/rails/application.rb:77:in `method_missing'
railties (3.0.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.1) lib/rack/content_length.rb:13:in `call'
rack (1.2.1) lib/rack/handler/webrick.rb:52:in `service'
/Users/swright/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/swright/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/swright/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Simple answer, if you try to session in your views it will access the Rails Session object, you have a naming problem on your hands.
Thats why the Hash error is coming, it tries to access the session Hash.
Try something like
<h1>List Sessions</h1>
<% #sessions.each do |s| %>
<div><%= s.title %></div>
<% end %>
And by the way, you might change your controller to:
class HomeController < ApplicationController
def index
#sessions = Session.all
end
end
For me the problem was the type was not defined in the xml tag
xml.instruct!
xml.posts("type"=>"array") do
#posts.each do |p|
xml.post do
xml.id("type"=>"integer") do
p.id
end
xml.title p.title
xml.content p.content
end
end
end
vs
xml.instruct!
xml.posts do
#posts.each do |p|
xml.post do
xml.id p.id
xml.title p.title
xml.content p.content
end
end
end
Hope this helps you
I had this problem and like Joel above, it was because the type wasn't defined. I found a solution here but briefly if you don't have any control over the XML you can define a custom format, and tell your ActiveResource class to use it:
class TaxAlert < ActiveResource::Base
protected
class CustomFormat
include ActiveResource::Formats::XmlFormat
def decode(xml)
ActiveResource::Formats::XmlFormat.decode(xml)['alert']
end
end
self.format = CustomFormat.new
end