Error using ActiveResource with Rails 3 and REST API - ruby-on-rails

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

Related

BSON::InvalidObjectId illegal ObjectId format Rails 3.1.1

I'm getting the following error when trying to use the delete method in a Ruby on Rails application.
BSON::InvalidObjectId in ServicesController#destroy
I'm using Rails 3.1.1 and MongoDB v2.4.15
Below is the Delete link in question.
%li= link_to 'Delete', #service, :method => :delete, :confirm => "Are you sure you want to delete this service?"
Here is the log error:
Started DELETE "/services/appeals" for 127.0.0.1 at 2016-12-21 11:08:42 -0500
Creating scope :orderable_scope. Overwriting existing method Service.orderable_scope.
Processing by ServicesController#destroy as HTML
Parameters: {"authenticity_token"=>"xxx", "id"=>"appeals"}
Completed 500 Internal Server Error in 11ms
BSON::InvalidObjectId (illegal ObjectId format: appeals):
When deleting items in other collections it works fine. I'm able to use the "services" edit link without errors. Below is the working Delete link and logs for deleting an attorney.
Working Link
%li= link_to "Delete", #attorney, :method => :delete, :confirm => "Are you sure you want to delete this attorney?"
Working Log
Started DELETE "/attorneys/first-last" for 127.0.0.1 at 2016-12-21 10:37:07 -0500
Creating scope :orderable_scope. Overwriting existing method Attorney.orderable_scope.Processing by AttorneysController#destroy as HTMLParameters: {"authenticity_token"=>"xxx", "id"=>"first-last"}
MONGODB x_development['system.namespaces'].find({})
MONGODB x_development['attorneys'].find({:slug=>"first-last"}).limit(-1).sort([[:_id, :asc]])
MONGODB x_development['system.namespaces'].find({})
MONGODB x_development['users'].find({:_id=>BSON::ObjectId('585a9dbc737da97cbd000006')}).limit(-1).sort([[:_id, :asc]])
Creating scope :orderable_scope. Overwriting existing method Service.orderable_scope.
MONGODB x_development['system.namespaces'].find({})
Redirected to http://0.0.0.0:3000/attorneys
Completed 302 Found in 69ms`
It's another developers application that I'm trying to get working correctly again. I'm not very experienced with Ruby, Rails, or MongoDB.
UPDATE
Below is the Full Trace
bson (1.4.0) lib/bson/types/object_id.rb:126:in `from_string'
mongoid (2.4.12) lib/mongoid/fields/internal/object_id.rb:38:in `serialize'
mongoid (2.4.12) lib/mongoid/criterion/optional.rb:97:in `for_ids'
mongoid (2.4.12) lib/mongoid/criteria.rb:310:in `search'
mongoid (2.4.12) lib/mongoid/criterion/inclusion.rb:125:in `find'
mongoid (2.4.12) lib/mongoid/finders.rb:84:in `find'
inherited_resources (1.3.1) lib/inherited_resources/base_helpers.rb:44:in `resource'
cancan (1.6.7) lib/cancan/inherited_resource.rb:11:in `load_resource_instance'
cancan (1.6.7) lib/cancan/controller_resource.rb:32:in `load_resource'
cancan (1.6.7) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancan (1.6.7) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (3.1.1) lib/active_support/callbacks.rb:439:in `_run__3707406960644643515__process_action__1333295842175195574__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'
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:131: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'
mongoid (2.4.12) lib/rack/mongoid/middleware/identity_map.rb:33:in `block in call'
mongoid (2.4.12) lib/mongoid.rb:133:in `unit_of_work'
mongoid (2.4.12) lib/rack/mongoid/middleware/identity_map.rb:33: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:35: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'
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/Home/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/Home/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/Home/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Anyone have any ideas? Any help would be greatly appreciated.
I found that the issue is in my services_controller
There was a line before_filter :load_service_by_slug, :only => [:edit, :update, :move_up, :move_down]
I had to add :destroy to that line.

Rails: wrong number of arguments (1 for 0) when calling save but passing no arguments

Running into a strange argument error. I get a wrong number of arguments (1 for 0) error when submitting a form. Logs report it's happening on the call to if #object.save but I'm not passing in any arguments. Here's the create method involved.
#venue_imports_controller.rb
def create
#venue_import = VenueImport.new
#file = params[:venue_import][:file]
Rails.logger.debug "--> Trying to read csv file..."
content = File.read(#file.tempfile)
Rails.logger.debug "--> Loading csv contents into content column..."
#venue_import.content = content
if #venue_import.save
Rails.logger.debug "--> Made it into the save "
process_files!
flash[:notice] = "CSV upload is being processed"
render :new
else
flash[:notice] = "CSV upload could not be processed"
render :new
end
end
#venue_import.rb
require 'csv'
class VenueImport < ActiveRecord::Base
include ActiveModel::Validations
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_accessor :file, :saved_venues, :unsaved_venues
def valid?
errors.empty?
end
def persisted?
false
end
end
#log output
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
--> Trying to read csv file...
--> Loading csv contents into content column...
(0.1ms) BEGIN
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 5ms
ArgumentError - wrong number of arguments (1 for 0):
activerecord (3.2.2) lib/active_record/validations.rb:77:in `perform_validations'
activerecord (3.2.2) lib/active_record/validations.rb:50:in `save'
activerecord (3.2.2) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (3.2.2) lib/active_record/transactions.rb:241:in `block (2 levels) in save'
activerecord (3.2.2) lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
activerecord (3.2.2) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.2) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.2.2) lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
activerecord (3.2.2) lib/active_record/transactions.rb:241:in `block in save'
activerecord (3.2.2) lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
activerecord (3.2.2) lib/active_record/transactions.rb:240:in `save'
app/controllers/admin/venues/venue_imports_controller.rb:15:in `create'
actionpack (3.2.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.2) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.2) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.2) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.2) lib/active_support/callbacks.rb:448:in `block in _run__2350649128043012098__process_action__1475487348551593386__callbacks'
activesupport (3.2.2) lib/active_support/callbacks.rb:215:in `block in _conditional_callback_around_451'
activesupport (3.2.2) lib/active_support/callbacks.rb:326:in `around'
activesupport (3.2.2) lib/active_support/callbacks.rb:310:in `_callback_around_13'
activesupport (3.2.2) lib/active_support/callbacks.rb:214:in `_conditional_callback_around_451'
activesupport (3.2.2) lib/active_support/callbacks.rb:414:in `_run__2350649128043012098__process_action__1475487348551593386__callbacks'
activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.2) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.2) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.2) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.2) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.2) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.2) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.2) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.2) lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
activerecord (3.2.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.2) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.2) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.2) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.2) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.2) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:67:in `dispatch'
actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:30:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:594:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.2) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `_run__9921957878612641__call__2635867529413993280__callbacks'
activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.2) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
airbrake (3.1.15) lib/airbrake/rails/middleware.rb:13:in `call'
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:in `better_errors_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.2) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.2) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.2) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
airbrake (3.1.15) lib/airbrake/user_informer.rb:16:in `_call'
airbrake (3.1.15) lib/airbrake/user_informer.rb:12:in `call'
railties (3.2.2) lib/rails/engine.rb:479:in `call'
railties (3.2.2) lib/rails/application.rb:220:in `call'
railties (3.2.2) lib/rails/railtie/configurable.rb:30:in `method_missing'
rack (1.4.5) lib/rack/deflater.rb:13:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.2) lib/rails/rack/log_tailer.rb:14:in `call'
thin (1.6.1) lib/thin/connection.rb:82:in `block in pre_process'
thin (1.6.1) lib/thin/connection.rb:80:in `pre_process'
thin (1.6.1) lib/thin/connection.rb:55:in `process'
thin (1.6.1) lib/thin/connection.rb:41:in `receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
thin (1.6.1) lib/thin/backends/base.rb:73:in `start'
thin (1.6.1) lib/thin/server.rb:162:in `start'
rack (1.4.5) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.5) lib/rack/server.rb:268:in `start'
railties (3.2.2) lib/rails/commands/server.rb:70:in `start'
railties (3.2.2) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.2) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `<main>'
script/rails:0:in `<main>'
Started POST "/__better_errors/70352114726040/variables" for 127.0.0.1 at 2014-07-31 16:28:14 -0700
I've tried taking out the call to process_files! to make sure that wasn't causing any issues. No luck.
You need to remove ActiveModel and just rely on ActiveRecord. ActiveRecord already includes these modules. The reason your save is failing is because ActiveRecord uses a chain of super calls to validate your models when you call save. When you include ActiveModel on top of ActiveRecord, it messes with the inheritance chain so super is calling the wrong save method.

Undefined method "reorder" for #<Array:0xbc38600> using ActiveAdmin

I have a problem with ActiveAdmin using Ruby ruby 1.9.2p290 and Ruby on Rails 3.2.1.
I used this wiki page to setup the resource for the AdminUsers that works perfectly: Your First Admin Resource: AdminUser
After that I wanted to create the resource for my project model. The migrations for that looks like this:
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :name
t.text :description
t.boolean :isactive
t.timestamps
end
end
end
class AddSlugToProjects < ActiveRecord::Migration
def change
add_column :projects, :slug, :string
add_index :projects, :slug
end
end
My project model:
class Project < ActiveRecord::Base
scope :isactive, :conditions => ["isactive = ?",true]
extend FriendlyId
friendly_id :name, use: [:slugged, :history]
attr_accessible :name, :description, :isactive
end
And that's the project_controller:
class ProjectsController < ApplicationController
def index
#
# #projects gets filled in application_controller to be always
# available for the twitter bootstrap navbar
# #projects = Project.isactive.find(:all, :order => 'LOWER(name) asc')
#
end
def show
#project = Project.isactive.find(params[:id])
if request.path != project_path(#project)
redirect_to #project, :status => :moved_permanently
end
end
end
The admin/projects.rb is currently 'empty'
ActiveAdmin.register Project do
end
And after all that, everything I get is the following error:
Started GET "/admin/projects" for 192.168.0.2 at 2012-02-21 20:58:49 +0100
Processing by Admin::ProjectsController#index as HTML
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE (isactive = 't') ORDER BY LOWER(name) asc
AdminUser Load (0.1ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = 2 LIMIT 1
Rendered /home/eins11/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activeadmin-0.4.1/app/views/active_admin/resource/index.html.arb (11.7ms)
Completed 500 Internal Server Error in 54ms
ActionView::Template::Error (undefined method `reorder' for #<Array:0xa3ecd44>):
1: render renderer_for(:index)
activeadmin (0.4.1) lib/active_admin/views/pages/index.rb:41:in `items_in_collection?'
activeadmin (0.4.1) lib/active_admin/views/pages/index.rb:20:in `main_content'
activeadmin (0.4.1) lib/active_admin/views/pages/base.rb:116:in `block (2 levels) in build_main_content_wrapper'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:68:in `block in build_tag'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:96:in `with_current_dom_context'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:67:in `build_tag'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:77:in `insert_tag'
activeadmin (0.4.1) lib/active_admin/arbre/html/element.rb:17:in `div'
activeadmin (0.4.1) lib/active_admin/views/pages/base.rb:115:in `block in build_main_content_wrapper'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:68:in `block in build_tag'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:96:in `with_current_dom_context'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:67:in `build_tag'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:77:in `insert_tag'
activeadmin (0.4.1) lib/active_admin/arbre/html/element.rb:17:in `div'
activeadmin (0.4.1) lib/active_admin/views/pages/base.rb:114:in `build_main_content_wrapper'
activeadmin (0.4.1) lib/active_admin/views/pages/base.rb:98:in `block in build_page_content'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:68:in `block in build_tag'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:96:in `with_current_dom_context'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:67:in `build_tag'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:77:in `insert_tag'
activeadmin (0.4.1) lib/active_admin/arbre/html/element.rb:17:in `div'
activeadmin (0.4.1) lib/active_admin/views/pages/base.rb:97:in `build_page_content'
activeadmin (0.4.1) lib/active_admin/views/pages/base.rb:41:in `block (2 levels) in build_page'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:68:in `block in build_tag'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:96:in `with_current_dom_context'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:67:in `build_tag'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:77:in `insert_tag'
activeadmin (0.4.1) lib/active_admin/arbre/html/element.rb:17:in `div'
activeadmin (0.4.1) lib/active_admin/views/pages/base.rb:38:in `block in build_page'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:96:in `with_current_dom_context'
activeadmin (0.4.1) lib/active_admin/views/pages/base.rb:37:in `build_page'
activeadmin (0.4.1) lib/active_admin/views/pages/base.rb:10:in `build'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:63:in `build_tag'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:77:in `insert_tag'
activeadmin (0.4.1) lib/active_admin/arbre/builder.rb:41:in `method_missing'
activeadmin (0.4.1) lib/active_admin/view_helpers/renderer_helper.rb:21:in `render'
activeadmin (0.4.1) app/views/active_admin/resource/index.html.arb:1:in `__home_eins____rbenv_versions_______p____lib_ruby_gems_______gems_activeadmin_______app_views_active_admin_resource_index_html_arb___523541447_100786690'
actionpack (3.2.1) lib/action_view/template.rb:143:in `block in render'
activesupport (3.2.1) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.1) lib/action_view/template.rb:141:in `render'
actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:41:in `block (2 levels) in render_template'
actionpack (3.2.1) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.1) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:40:in `block in render_template'
actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:48:in `render_with_layout'
actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:39:in `render_template'
actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:12:in `render'
actionpack (3.2.1) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.2.1) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.2.1) lib/abstract_controller/rendering.rb:109:in `_render_template'
actionpack (3.2.1) lib/action_controller/metal/streaming.rb:225:in `_render_template'
actionpack (3.2.1) lib/abstract_controller/rendering.rb:103:in `render_to_body'
actionpack (3.2.1) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
actionpack (3.2.1) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
actionpack (3.2.1) lib/abstract_controller/rendering.rb:88:in `render'
actionpack (3.2.1) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.2.1) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/home/eins11/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:310:in `realtime'
activesupport (3.2.1) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
activerecord (3.2.1) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:39:in `render'
activeadmin (0.4.1) lib/active_admin/resource_controller/actions.rb:13:in `block (2 levels) in index'
actionpack (3.2.1) lib/action_controller/metal/responder.rb:229:in `call'
actionpack (3.2.1) lib/action_controller/metal/responder.rb:229:in `default_render'
actionpack (3.2.1) lib/action_controller/metal/responder.rb:160:in `to_html'
responders (0.6.5) lib/responders/flash_responder.rb:93:in `to_html'
actionpack (3.2.1) lib/action_controller/metal/responder.rb:153:in `respond'
actionpack (3.2.1) lib/action_controller/metal/responder.rb:146:in `call'
actionpack (3.2.1) lib/action_controller/metal/mime_responds.rb:238:in `respond_with'
inherited_resources (1.3.0) lib/inherited_resources/actions.rb:7:in `index'
activeadmin (0.4.1) lib/active_admin/resource_controller/actions.rb:11:in `index'
actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.1) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.1) lib/active_support/callbacks.rb:469:in `_run__639476650__process_action__1068992826__callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
activerecord (3.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.1) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.1) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.1) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.1) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `dispatch'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:30:in `call'
journey (1.0.1) lib/journey/router.rb:68:in `block in call'
journey (1.0.1) lib/journey/router.rb:56:in `each'
journey (1.0.1) lib/journey/router.rb:56:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in `call'
sass (3.1.15) lib/sass/plugin/rack.rb:54:in `call'
warden (1.1.1) lib/warden/manager.rb:35:in `block in call'
warden (1.1.1) lib/warden/manager.rb:34:in `catch'
warden (1.1.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.1) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `_run__471556791__call__937320005__callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.1) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.2.1) lib/rails/engine.rb:479:in `call'
railties (3.2.1) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/home/eins11/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/home/eins11/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/home/eins11/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Rendered /home/eins11/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (5.1ms)
Rendered /home/eins11/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms)
Rendered /home/eins11/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.8ms)
And I have no idea what the problem is...
Ah, I've found the solution.
I don't know exactly why the error happened but I had to change my application_controller.
In the application_controller I fetched all projects to #projects:
before_filter :getActiveProjects
protected
def getActiveProjects
#projects = Project.isactive.find(:all, :order => 'LOWER(name) asc')
end
That was confusing ActiveAdmin. After I changed that to
before_filter :getActiveProjects
protected
def getActiveProjects
#projects_all = Project.isactive.find(:all, :order => 'LOWER(name) asc')
end
and projects_controller index action to
def index
#projects = #projects_all
end
everything worked fine ;)
An easier solution is to just skip the filter in the particular active admin controller:
ActiveAdmin.register Project do
controller do
skip_before_filter :getActiveProjects
end
end
The active admin make special processing of #resource variables. Where resource is correponding class name passed to register method. So for code:
ActiveAdmin.register Project do
end
name of variable is
`#project`
We must not assign value to variable with such name. Also such varibles may be object of ActiveRecord::Relation class. And how #Oliver write the name like #projects_all is good choice. The using of skip_before_filter is solve the problem too.
One crafty thing need to check. The controller of resource may not have any filters. The controller of resource may not exists at all. What is the filter name to put to skip_before_filter? And where is such filters? The default application controller is used anyway. The filter may be installed at main application controller. The error message of this problem is not informative at all. So need to check application controller for installed filters too.

NoMethodError in Ruby on Rails calling Class Method from a controller

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.

Rails Authorization with CanCan Problem

On my rails app I have implemented AuthLogic and CanCan. However when trying to figure out if the user can manage an article (checks if he is owner through the article.user_id) with CanCan I am running into issues. This should be straight forward I don't know what I'm doing wrong.
User has_many Articles
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :read, :all
can :manage, Article do |article|
article.user_id == user.id
end
end
end
I get the following error when going to /articles/index
undefined method `user' for #<Array:0x1035c5158>
Extracted source (around line #15):
12: <td><%= article.created_at %></td>
13: <td><%= article.user.login %></td>
14: <td><%= link_to 'View', article %></td>
15: <% if can? :manage, article %>
16: <td><%= link_to 'Edit', edit_article_path(article) %></td>
17: <% end %>
18: <% if can? :manage, article %>
app/models/ability.rb:9:in `try'
app/models/ability.rb:9:in `initialize'
cancan (1.3.4) lib/cancan/can_definition.rb:101:in `call'
cancan (1.3.4) lib/cancan/can_definition.rb:101:in `call_block'
cancan (1.3.4) lib/cancan/can_definition.rb:29:in `matches_conditions?'
cancan (1.3.4) lib/cancan/ability.rb:54:in `can?'
authlogic (2.1.6) lib/authlogic/session/password.rb:127:in `detect'
cancan (1.3.4) lib/cancan/ability.rb:53:in `each'
cancan (1.3.4) lib/cancan/ability.rb:53:in `detect'
cancan (1.3.4) lib/cancan/ability.rb:53:in `can?'
cancan (1.3.4) lib/cancan/controller_additions.rb:228:in `can?'
actionpack (3.0.0) lib/abstract_controller/helpers.rb:55:in `send'
actionpack (3.0.0) lib/abstract_controller/helpers.rb:55:in `can?'
app/views/articles/_articles.html.erb:15:in `_app_views_articles__articles_html_erb__1624837607_2174751840_1924990'
app/views/articles/_articles.html.erb:8:in `each'
app/views/articles/_articles.html.erb:8:in `_app_views_articles__articles_html_erb__1624837607_2174751840_1924990'
actionpack (3.0.0) lib/action_view/template.rb:135:in `send'
actionpack (3.0.0) lib/action_view/template.rb:135:in `render'
activesupport (3.0.0) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.0) lib/action_view/template.rb:127:in `render'
actionpack (3.0.0) lib/action_view/render/partials.rb:294:in `render_partial'
actionpack (3.0.0) lib/action_view/render/partials.rb:223:in `render'
activesupport (3.0.0) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.0) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.0) lib/action_view/render/partials.rb:221:in `render'
actionpack (3.0.0) lib/action_view/render/partials.rb:334:in `_render_partial'
actionpack (3.0.0) lib/action_view/render/rendering.rb:31:in `render'
app/views/articles/index.html.erb:5:in `_app_views_articles_index_html_erb___1319251192_2174766940_0'
actionpack (3.0.0) lib/action_view/template.rb:135:in `send'
actionpack (3.0.0) lib/action_view/template.rb:135:in `render'
activesupport (3.0.0) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.0) lib/action_view/template.rb:127:in `render'
actionpack (3.0.0) lib/action_view/render/rendering.rb:59:in `_render_template'
activesupport (3.0.0) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.0) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.0) lib/action_view/render/rendering.rb:56:in `_render_template'
actionpack (3.0.0) lib/action_view/render/rendering.rb:26:in `render'
actionpack (3.0.0) lib/abstract_controller/rendering.rb:114:in `_render_template'
actionpack (3.0.0) lib/abstract_controller/rendering.rb:108:in `render_to_body'
actionpack (3.0.0) lib/action_controller/metal/renderers.rb:47:in `render_to_body'
actionpack (3.0.0) lib/action_controller/metal/compatibility.rb:55:in `render_to_body'
actionpack (3.0.0) lib/abstract_controller/rendering.rb:101:in `render_to_string'
actionpack (3.0.0) lib/abstract_controller/rendering.rb:92:in `render'
actionpack (3.0.0) lib/action_controller/metal/rendering.rb:17:in `render'
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
activesupport (3.0.0) lib/active_support/core_ext/benchmark.rb:5:in `ms'
/opt/local/lib/ruby/1.8/benchmark.rb:308:in `realtime'
activesupport (3.0.0) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
activerecord (3.0.0) lib/active_record/railties/controller_runtime.rb:15:in `cleanup_view_runtime'
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.0.0) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.0) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.0) lib/abstract_controller/callbacks.rb:18:in `process_action'
activesupport (3.0.0) lib/active_support/callbacks.rb:440:in `_run__704842227__process_action__199225275__callbacks'
activesupport (3.0.0) lib/active_support/callbacks.rb:409:in `send'
activesupport (3.0.0) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.0) lib/active_support/callbacks.rb:93:in `send'
activesupport (3.0.0) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
activesupport (3.0.0) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.0) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.0) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.0) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.0) lib/abstract_controller/rendering.rb:40:in `process'
actionpack (3.0.0) lib/action_controller/metal.rb:133:in `dispatch'
actionpack (3.0.0) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.0) lib/action_controller/metal.rb:173:in `action'
actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `call'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93: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.0) lib/action_dispatch/routing/route_set.rb:492:in `call'
actionpack (3.0.0) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.0) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.1) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.0) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.0) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.0) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.0) lib/action_dispatch/middleware/cookies.rb:287:in `call'
activerecord (3.0.0) lib/active_record/query_cache.rb:32:in `call'
activerecord (3.0.0) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.0) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.0) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `call'
actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:46:in `call'
activesupport (3.0.0) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'
actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.1) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.0) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.0) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'
railties (3.0.0) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.0) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.1) lib/rack/lock.rb:11:in `call'
rack (1.2.1) lib/rack/lock.rb:11:in `synchronize'
rack (1.2.1) lib/rack/lock.rb:11:in `call'
actionpack (3.0.0) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.0) lib/rails/application.rb:168:in `call'
railties (3.0.0) lib/rails/application.rb:77:in `send'
railties (3.0.0) lib/rails/application.rb:77:in `method_missing'
railties (3.0.0) 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'
/opt/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/opt/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/opt/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/opt/local/lib/ruby/1.8/webrick/server.rb:162:in `start'
/opt/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/opt/local/lib/ruby/1.8/webrick/server.rb:95:in `start'
/opt/local/lib/ruby/1.8/webrick/server.rb:92:in `each'
/opt/local/lib/ruby/1.8/webrick/server.rb:92:in `start'
/opt/local/lib/ruby/1.8/webrick/server.rb:23:in `start'
/opt/local/lib/ruby/1.8/webrick/server.rb:82:in `start'
rack (1.2.1) lib/rack/handler/webrick.rb:13:in `run'
rack (1.2.1) lib/rack/server.rb:213:in `start'
railties (3.0.0) lib/rails/commands/server.rb:65:in `start'
railties (3.0.0) lib/rails/commands.rb:30
railties (3.0.0) lib/rails/commands.rb:27:in `tap'
railties (3.0.0) lib/rails/commands.rb:27
script/rails:6:in `require'
script/rails:6
Here is my view:
<% if can? :manage, article %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<% end %>
<% if can? :manage, article %>
<td><%= link_to 'Delete', article, :method => :delete %></td>
<% end %>
Here is my controller:
def index
#articles = Article.find(:all)
end
Not sure exactly what your problem cause is without a trace, but I find I often have to check for nil when using a value, i.e. in your example:
can :manage, Article do |article|
article && article.user_id == user.id
end
it seems that in certain situations the block argument is nil
According to http://github.com/ryanb/cancan/wiki/defining-abilities-with-hashes there may be a cleaner way:
can :manage, Article, :user_id => user.id
I would try that method first, see how you go.

Resources