I want to email users every time a new treatment that is under the category they are interested in is added.
I have a join table interests_users which is holds a user_id and a interest_id. Treatments has a variable called category which is used to compare against interests.
This is what the create block in my treatments controller looks like.
def create
#treatment = Treatment.new(treatment_params)
respond_to do |format|
if #treatment.save
interest = Interest.find_by(name: #treatment.category)
if interest
User.find_by_sql("SELECT * FROM users
INNER JOIN interests_users ON clients.id = interests_users.client_id
WHERE interests_users.interest_id = ?", interest.id).each do |user|
Bestevermailer.treatmentNotification(user, #treatment).deliver
end
end
format.html { redirect_to #treatment, notice: 'Treatment was successfully created.' }
format.json { render :show, status: :created, location: #treatment }
else
format.html { render :new }
format.json { render json: #treatment.errors, status: :unprocessable_entity }
end
end
end
Full Trace
activerecord (4.2.5) lib/active_record/connection_adapters/sqlite3_adapter.rb:289:in `exec_query'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/database_statements.rb:355:in `select'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/query_cache.rb:68:in `block in select_all'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/query_cache.rb:83:in `cache_sql'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/query_cache.rb:68:in `select_all'
activerecord (4.2.5) lib/active_record/querying.rb:39:in `find_by_sql'
app/controllers/treatments_controller.rb:72:in `block in create'
actionpack (4.2.5) lib/action_controller/metal/mime_responds.rb:212:in `respond_to'
app/controllers/treatments_controller.rb:68:in `create'
actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.5) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.5) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.5) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:76:in `call'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:817:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.5) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.5) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.5) lib/rails/engine.rb:518:in `call'
railties (4.2.5) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Try re-framing your query. Here:
User.joins('INNER JOIN interests_users ON clients.id = interests_users.client_id').where("interests_users.interest_id = ?", interest.id)
As per a similar SO:
find_by_sql doesn't work well with query placeholders
find_by_sql expects an array as a parameter:
User.find_by_sql([
"SELECT * FROM users
INNER JOIN interests_users
ON clients.id = interests_users.client_id
WHERE interests_users.interest_id = ?",
interest.id]).each do |user|
Related
I have a Rails app and have implemented geolocalization: with geocoder gem, I detect the ip and know which country the user is accessing pages from.
I'd like to add a 'country' attribute to the User table.
Users would come to the sign-up page and without them knowing I would detect for example they are accessing the app from Finland.
Then when they sign up in by filling in the 2 fields email & password, I would save email and password but I want to save/store the country I get using geocoder in this column 'country'.
How can I save this 'country' attribute if I don't have the field in the form ?
Sounds easy but I only know how to save values when there actually is a form field the user fills in.
Here what I did:
/app/controllers/confirmations_controller.rb
def after_sign_up_path_for(resource)
if Devise.allow_insecure_sign_in_after_confirmation
resource.update(user_country: request.location.country)
after_sign_in_path_for(resource)
else
resource.update(user_country: request.location.country)
root_path
end
end
What's weird is that the user is saved with everything fine (including location) on my local database. But then something must happen as I get an error page with the following error message:
undefined method `model_name' for TrueClass:Class
The whole error log
(0.8ms) COMMIT
(0.5ms) BEGIN
SQL (1.3ms) UPDATE "users" SET "remember_created_at" = $1, "updated_at" = $2 WHERE "users"."id" = 427 [["remember_created_at", 2014-06-24 10:52:32 UTC], ["updated_at", 2014-06-24 12:52:32 +0200]]
(0.7ms) COMMIT
(0.4ms) BEGIN
SQL (0.9ms) UPDATE "users" SET "last_sign_in_at" = $1, "current_sign_in_at" = $2, "last_sign_in_ip" = $3, "current_sign_in_ip" = $4, "sign_in_count" = $5, "updated_at" = $6 WHERE "users"."id" = 427 [["last_sign_in_at", 2014-06-24 10:52:32 UTC], ["current_sign_in_at", 2014-06-24 10:52:32 UTC], ["last_sign_in_ip", "24.193.83.1"], ["current_sign_in_ip", "24.193.83.1"], ["sign_in_count", 1], ["updated_at", 2014-06-24 12:52:32 +0200]]
(0.7ms) COMMIT
(0.4ms) BEGIN
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('mathieu.proffit75++edaaaaaa+++#gmail.com') AND "users"."id" != 427) LIMIT 1
SQL (1.3ms) UPDATE "users" SET "user_country" = $1, "updated_at" = $2 WHERE "users"."id" = 427 [["user_country", "United States"], ["updated_at", 2014-06-24 12:52:32 +0200]]
(0.7ms) COMMIT
Completed 500 Internal Server Error in 4984ms
NoMethodError - undefined method `model_name' for TrueClass:Class:
actionpack (4.0.5) lib/action_controller/model_naming.rb:9:in `model_name_from_record_or_class'
actionpack (4.0.5) lib/action_dispatch/routing/polymorphic_routes.rb:182:in `build_named_route_call'
actionpack (4.0.5) lib/action_dispatch/routing/polymorphic_routes.rb:120:in `polymorphic_url'
actionpack (4.0.5) lib/action_dispatch/routing/url_for.rb:159:in `url_for'
actionpack (4.0.5) lib/action_controller/metal/rendering.rb:68:in `_process_options'
actionpack (4.0.5) lib/action_controller/metal/streaming.rb:202:in `_process_options'
actionpack (4.0.5) lib/abstract_controller/rendering.rb:119:in `render_to_body'
actionpack (4.0.5) lib/action_controller/metal/rendering.rb:33:in `render_to_body'
actionpack (4.0.5) lib/action_controller/metal/renderers.rb:26:in `render_to_body'
actionpack (4.0.5) lib/abstract_controller/rendering.rb:97:in `render'
actionpack (4.0.5) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.0.5) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.0.5) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/mreisner/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
activesupport (4.0.5) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.0.5) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.0.5) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.0.5) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.0.5) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.0.5) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.0.5) lib/action_controller/metal/responder.rb:233:in `default_render'
actionpack (4.0.5) lib/action_controller/metal/responder.rb:161:in `to_html'
actionpack (4.0.5) lib/action_controller/metal/responder.rb:154:in `respond'
actionpack (4.0.5) lib/action_controller/metal/responder.rb:147:in `call'
actionpack (4.0.5) lib/action_controller/metal/mime_responds.rb:330:in `respond_with'
devise (3.2.4) app/controllers/devise/registrations_controller.rb:20:in `create'
actionpack (4.0.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.5) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.5) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.5) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.5) lib/active_support/callbacks.rb:483:in `_run__1192109421260337467__process_action__callbacks'
activesupport (4.0.5) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.5) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.5) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.5) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.5) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.0.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.5) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.5) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.5) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.5) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.5) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.5) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.5) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.5) lib/action_dispatch/routing/mapper.rb:44:in `call'
actionpack (4.0.5) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.5) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.5) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.5) lib/action_dispatch/routing/route_set.rb:674: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 `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.5) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.5) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.5) lib/active_record/migration.rb:373:in `call'
actionpack (4.0.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.5) lib/active_support/callbacks.rb:373:in `_run__3240580690818246217__call__callbacks'
activesupport (4.0.5) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.5) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.5) lib/action_dispatch/middleware/remote_ip.rb:76: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 (4.0.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.0.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.0.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.0.5) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
actionpack (4.0.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.5) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.5) lib/rails/engine.rb:511:in `call'
railties (4.0.5) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/mreisner/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/home/mreisner/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/home/mreisner/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
I'd probably try to catch the user after they sign up. You can read more on how to do it in the docs Devise: How to Redirect after Registration
Override the Devise Registration Controller with something like this:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
resource.update(location: request.location)
root_path
end
end
That is untested code but should get you in the right direction. The second line of the method returns a redirect to the root_path after signup which you might want to change.
I'm new to Ruby on Rails and trying to create a relatively simple web application by following this guide: http://www.sitepoint.com/youtube-rails/. I've come to a point where all the code is in place but still getting the NoMethodError in VideosController#create:
undefined method `uid=' for #<Video:0x007fb066c41d28>
app/controllers/videos_controller.rb
class VideosController < ApplicationController
def index
#videos = Video.order('created_at DESC')
end
def new
#video = Video.new
end
def create
#video = Video.new(params[:video])
if #video.save
flash[:success] = 'Video added!'
redirect_to root_url
else
render 'new'
end
end
end
app/models/video.rb
class Video < ActiveRecord::Base
attr_accessible :link
YT_LINK_FORMAT = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/i
validates :link, presence: true, format: YT_LINK_FORMAT
before_create -> do
uid = link.match(YT_LINK_FORMAT)
self.uid = uid[2] if uid && uid[2]
if self.uid.to_s.length != 11
self.errors.add(:link, 'is invalid.')
false
elsif Video.where(uid: self.uid).any?
self.errors.add(:link, 'is not unique.')
false
else
get_additional_info
end
end
private
def get_additional_info
begin
client = YouTubeIt::OAuth2Client.new(dev_key: '<my_dev_key>')
video = client.video_by(uid)
self.title = video.title
self.duration = parse_duration(video.duration)
self.author = video.author.name
self.likes = video.rating.likes
self.dislikes = video.rating.dislikes
rescue
self.title = '' ; self.duration = '00.00.00' ; self.author = '' ; self.likes = 0 ; self.dislikes = 0
end
end
def parse_duration(d)
hr = (d / 3600).floor
min = ((d - (hr * 3600)) / 60).floor
sec = (d - (hr * 3600) - (min * 60)).floor
hr = '0' + hr.to_s if hr.to_i < 10
min = '0' + min.to_s if min.to_i < 10
sec = '0' + sec.to_s if sec.to_i < 10
hr.to_s + ':' + min.to_s + ':' + sec.to_s
end
end
Full Trace
activemodel (3.2.17) lib/active_model/attribute_methods.rb:407:in `method_missing'
activerecord (3.2.17) lib/active_record/attribute_methods.rb:149:in `method_missing'
app/models/video.rb:10:in `block in <class:Video>'
activesupport (3.2.17) lib/active_support/callbacks.rb:407:in `_run__3695319422649908747__create__1619689881822406221__callbacks'
activesupport (3.2.17) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.17) lib/active_support/callbacks.rb:385:in `_run_create_callbacks'
activesupport (3.2.17) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.17) lib/active_record/callbacks.rb:268:in `create'
activerecord (3.2.17) lib/active_record/persistence.rb:348:in `create_or_update'
activerecord (3.2.17) lib/active_record/callbacks.rb:264:in `block in create_or_update'
activesupport (3.2.17) lib/active_support/callbacks.rb:403:in `_run__3695319422649908747__save__1619689881822406221__callbacks'
activesupport (3.2.17) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.17) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.17) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.17) lib/active_record/callbacks.rb:264:in `create_or_update'
activerecord (3.2.17) lib/active_record/persistence.rb:84:in `save'
activerecord (3.2.17) lib/active_record/validations.rb:50:in `save'
activerecord (3.2.17) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (3.2.17) lib/active_record/transactions.rb:259:in `block (2 levels) in save'
activerecord (3.2.17) lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
activerecord (3.2.17) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.17) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.2.17) lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
activerecord (3.2.17) lib/active_record/transactions.rb:259:in `block in save'
activerecord (3.2.17) lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
activerecord (3.2.17) lib/active_record/transactions.rb:258:in `save'
app/controllers/videos_controller.rb:12:in `create'
actionpack (3.2.17) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.17) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.17) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.17) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.17) lib/active_support/callbacks.rb:414:in `_run__333554687839828986__process_action__1999722818964258122__callbacks'
activesupport (3.2.17) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.17) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.17) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.17) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.17) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.17) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.17) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.17) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.17) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.17) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.17) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.17) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.17) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.17) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.17) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.17) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.17) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:36: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 `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:608:in `call'
actionpack (3.2.17) 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.17) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.17) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.17) 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.17) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.17) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.17) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.17) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.17) lib/active_support/callbacks.rb:405:in `_run__469011070482330715__call__1619689881822406221__callbacks'
activesupport (3.2.17) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.17) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.17) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.17) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.17) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.17) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.17) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.17) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.17) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.17) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.17) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.17) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.17) 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.17) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.17) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.17) lib/rails/engine.rb:484:in `call'
railties (3.2.17) lib/rails/application.rb:231:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.17) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/Users/<User_Name>/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/Kappe/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/Kappe/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
I hope I've given enough information. I think something is going on in video.rb where uid and self.uid are set but I don't have enough knowledge to solve this problem. Can you help me with this? Thanks in advance!
undefined method `uid=' for #
The error is because there is no uid attribute in videos table.
You should add that attribute through migration
AddUidToVideo uid:string
this will create a migration file for adding a uid column to that table then
do rake db:migrate
I can't seem to get CanCan working. This has turned out to be quite frustrating, especially when I have followed the rule book and tried, seemingly, all the tricks under the sun to get it to work.
I am using Rails 3.1.2, Devise 2.1.2, and CanCan 1.6.8. I set-up a separate role model as described by RyanB here.
This is my code.
1) ability.rb
class Ability
include CanCan::Ability
def initializer(user)
user ||= User.new # For users with no account
if (user.has_role?(:admin)) || (user.has_role?(:mod))
can :manage, :all
elsif user.has_role?(:registered)
can :read, :all
can :create, :all
can :update, Question do |question|
question.try(:user) == (user || user.has_role?(:admin) || user.has_role?(:mod))
end
can :destroy, Question do |question|
question.try(:user) == (user || user.has_role?(:admin) || user.has_role?(:mod))
end
else
can :read, :all
end
end
end
2) has_role? definition in user.rb
def has_role?(role_sym)
roles.any? { |r| r.name.underscore.to_sym == role_sym }
end
3) Controller before filters
before_filter :authenticate_user!, :except => :show
load_and_authorize_resource
skip_authorize_resource :only => :show
Authorizing any controller with CanCan results in the same error on all actions.
EDIT: Stack Trace
cancan (1.6.8) lib/cancan/controller_additions.rb:352:in `initialize'
cancan (1.6.8) lib/cancan/controller_additions.rb:352:in `new'
cancan (1.6.8) lib/cancan/controller_additions.rb:352:in `current_ability'
cancan (1.6.8) lib/cancan/controller_resource.rb:207:in `current_ability'
cancan (1.6.8) lib/cancan/controller_resource.rb:77:in `load_collection?'
cancan (1.6.8) lib/cancan/controller_resource.rb:33:in `load_resource'
cancan (1.6.8) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancan (1.6.8) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (3.2.1) lib/active_support/callbacks.rb:429:in `_run__458654864__process_action__156152648__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.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.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__446710069__call__461654473__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/administrator/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/home/administrator/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/home/administrator/.rvm/rubies/ruby-1
.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Okay, it seems, I did not followed ALL the rules of the rule book. Although I cannot see how this could be a problem. To get this working, I deleted my ability.rb file that I created manually, and used the rails g cancan:ability command to generate the same file in the same directory, and used the same code. And now it works like a charm.
I just installed delayed job on my little Rails 3.2 app. Here is the controller where I use it:
require 'waveform'
class MixesController < ApplicationController
def index
end
def new
#mix = Mix.new
end
def create
#mix = Mix.new(params[:mix])
if #mix.save
options = {
method: :rms,
width: 1800,
height: 280,
background_color: "#ffffff",
color: "#3333ff",
force: true,
quiet: true
}
waveform = "./public/waveforms/#{File.basename(#mix.sound_file.to_s, File.extname(#mix.sound_file.to_s))}.png"
::Waveform.new("./public" + #mix.sound_file.to_s, nil).delay.generate(waveform, options)
flash[:success] = "Mix successfully uploaded"
redirect_to mix_url(#mix)
else
render :new
end
end
def show
#mix = Mix.find(params[:id])
end
end
I get an error saying: undefined methodname' for nil:NilClass`, here is the full stack trace:
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:25:in `block in initialize'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `yield'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `default'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `block in initialize'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `yield'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `default'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `block in initialize'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `yield'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `default'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `block in initialize'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `yield'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `default'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:27:in `block in initialize'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `yield'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `default'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `accept'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:447:in `block in dump_ivars'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:445:in `each'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:445:in `dump_ivars'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:124:in `visit_Object'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `accept'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:434:in `block in emit_coder'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:432:in `each'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:432:in `emit_coder'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:417:in `dump_coder'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:100:in `accept'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:66:in `push'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych.rb:242:in `dump'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/core_ext.rb:14:in `psych_to_yaml'
delayed_job (3.0.3) lib/delayed/backend/base.rb:80:in `payload_object='
activerecord (3.2.3) lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
activerecord (3.2.3) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (3.2.3) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (3.2.3) lib/active_record/base.rb:498:in `initialize'
delayed_job (3.0.3) lib/delayed/backend/base.rb:28:in `new'
delayed_job (3.0.3) lib/delayed/backend/base.rb:28:in `enqueue'
delayed_job (3.0.3) lib/delayed/message_sending.rb:13:in `method_missing'
app/controllers/mixes_controller.rb:25:in `create'
actionpack (3.2.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.3) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.3) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.3) lib/active_support/callbacks.rb:414:in `_run__842168672059661955__process_action__4066633601342968686__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.3) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.3) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.3) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
activerecord (3.2.3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.3) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.3) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.3) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.3) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.3) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.3) lib/journey/router.rb:68:in `block in call'
journey (1.0.3) lib/journey/router.rb:56:in `each'
journey (1.0.3) lib/journey/router.rb:56:in `call'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:600:in `call'
actionpack (3.2.3) 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:35:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.3) 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.3) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.3) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:467:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `_run__647780415925652457__call__1911592835196459847__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.3) 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.3) 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.3) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.3) lib/rails/engine.rb:479:in `call'
railties (3.2.3) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/aziz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Does anyone know why I am getting this error please?
This is happening because the system is trying to turn nil into a value that can be stored in YAML and is failing.
Looking at your code, I expect that is because you are passing a nil object into the variables passed to Waveform.new (the method whose operation is being delayed). The variables passed to new will need to be stored until delayed job is ready to run the process, and I expect they are stored as YAML.
Can you refactor Waveform to either make the second attribute optional (so its value doesn't have to be preserved)
::Waveform.new("./public" + #mix.sound_file.to_s).delay.generate(waveform, options)
or use something else to have the same meaning as nil. For example, pass in the symbol :none. Then the line becomes:
::Waveform.new("./public" + #mix.sound_file.to_s, :none).delay.generate(waveform, options)
I'm attempting to get CanCan working for a few specific actions in my Rails 3 app. I've defined my roles in the Ability.rb class, but something is causing an error in both production and development. Specifically, when I call can? to show/hide a link depending on User role I get the following:
ArgumentError in Higher_eds#show
Showing /Users/me/Desktop/myapp/app/views/higher_eds/_logged_in.html.erb where line #62 raised:
wrong number of arguments (2 for 1)
Can anyone help me figure out what's going on?
I'm getting the error by calling the following in my /higher_eds/_logged_in.html.erb view:
<% if can? :highered_students, #highered %>
<li><%= link_to "Students", highered_students_higher_ed_path, :class=> 'inactive', :remote => true %></li>
<% end %>
In my highered_students action in higher_eds_controller.rb:
def highered_students
#highered = HigherEd.find(params[:id])
#profiles = Profile.where %[higher_ed = ? AND EXTRACT(year FROM college_grad_year) > EXTRACT(year FROM current_date)], #highered.name
respond_to do |format|
format.js { render :layout => nil }
end
end
My Ability.rb:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new #guest user
if user.role? :highered_admin
can :highered_students, HigherEd
end
if user.role? :admin
can :manage, :all
end
end
end
My full trace:
activerecord (3.0.5) lib/active_record/attribute_methods/query.rb:34:in `attribute?'
activemodel (3.0.5) lib/active_model/attribute_methods.rb:362:in `method_missing'
activerecord (3.0.5) lib/active_record/attribute_methods.rb:46:in `method_missing'
app/models/ability.rb:6:in `initialize'
cancan (1.6.7) lib/cancan/controller_additions.rb:349:in `new'
cancan (1.6.7) lib/cancan/controller_additions.rb:349:in `current_ability'
cancan (1.6.7) lib/cancan/controller_additions.rb:372:in `can?'
actionpack (3.0.5) lib/abstract_controller/helpers.rb:55:in `can?'
app/views/higher_eds/_logged_in.html.erb:62:in `block in _app_views_higher_eds__logged_in_html_erb__1309160371424325548_70132578271280__1361790216911758374'
actionpack (3.0.5) lib/action_view/helpers/capture_helper.rb:40:in `block in capture'
actionpack (3.0.5) lib/action_view/helpers/capture_helper.rb:172:in `with_output_buffer'
actionpack (3.0.5) lib/action_view/helpers/capture_helper.rb:40:in `capture'
actionpack (3.0.5) lib/action_view/helpers/tag_helper.rb:77:in `content_tag'
actionpack (3.0.5) lib/action_view/helpers/record_tag_helper.rb:59:in `content_tag_for'
actionpack (3.0.5) lib/action_view/helpers/record_tag_helper.rb:21:in `div_for'
app/views/higher_eds/_logged_in.html.erb:1:in `_app_views_higher_eds__logged_in_html_erb__1309160371424325548_70132578271280__1361790216911758374'
actionpack (3.0.5) lib/action_view/template.rb:135:in `block in render'
activesupport (3.0.5) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.5) lib/action_view/template.rb:127:in `render'
actionpack (3.0.5) lib/action_view/render/partials.rb:333:in `render_partial'
actionpack (3.0.5) lib/action_view/render/partials.rb:262:in `block in render'
activesupport (3.0.5) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.5) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.5) lib/action_view/render/partials.rb:260:in `render'
actionpack (3.0.5) lib/action_view/render/partials.rb:378:in `_render_partial'
actionpack (3.0.5) lib/action_view/render/rendering.rb:22:in `render'
app/views/higher_eds/show.html.erb:2:in `_app_views_higher_eds_show_html_erb__2358915598045819601_70132577771800__3922384299851769153'
actionpack (3.0.5) lib/action_view/template.rb:135:in `block in render'
activesupport (3.0.5) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.5) lib/action_view/template.rb:127:in `render'
actionpack (3.0.5) lib/action_view/render/rendering.rb:59:in `block in _render_template'
activesupport (3.0.5) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.5) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.5) lib/action_view/render/rendering.rb:56:in `_render_template'
actionpack (3.0.5) lib/action_view/render/rendering.rb:26:in `render'
actionpack (3.0.5) lib/abstract_controller/rendering.rb:115:in `_render_template'
actionpack (3.0.5) lib/abstract_controller/rendering.rb:109:in `render_to_body'
actionpack (3.0.5) lib/action_controller/metal/renderers.rb:47:in `render_to_body'
actionpack (3.0.5) lib/action_controller/metal/compatibility.rb:55:in `render_to_body'
actionpack (3.0.5) lib/abstract_controller/rendering.rb:102:in `render_to_string'
actionpack (3.0.5) lib/abstract_controller/rendering.rb:93:in `render'
actionpack (3.0.5) lib/action_controller/metal/rendering.rb:17:in `render'
actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.0.5) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:310:in `realtime'
activesupport (3.0.5) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
activerecord (3.0.5) lib/active_record/railties/controller_runtime.rb:15:in `cleanup_view_runtime'
actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:39:in actionpack `render'
app/controllers/higher_eds_controller.rb:42:in `show'
actionpack (3.0.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.5) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.5) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.5) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.5) lib/active_support/callbacks.rb:440:in `_run__4596096755335087880__process_action__1011169593507530898__callbacks'
activesupport (3.0.5) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.5) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.5) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.5) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.5) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.5) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.5) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.5) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.5) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.5) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.5) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.5) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.5) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.5) 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:145: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.5) lib/action_dispatch/routing/route_set.rb:492:in `call'
actionpack (3.0.5) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.5) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.5) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.5) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.5) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.5) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.5) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.5) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.5) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.5) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.5) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.5) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.5) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'
actionpack (3.0.5) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.5) lib/rack/sendfile.rb:106:in `call'
actionpack (3.0.5) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.5) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.5) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.5) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.5) lib/rack/lock.rb:13:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.5) lib/rack/lock.rb:13:in `call'
actionpack (3.0.5) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.5) lib/rails/application.rb:168:in `call'
railties (3.0.5) lib/rails/application.rb:77:in `method_missing'
railties (3.0.5) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.5) lib/rack/content_length.rb:13:in `call'
rack (1.2.5) lib/rack/handler/webrick.rb:52:in `service'
/Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
If I change my Ability.rb logic for checking if a User has a certain role, it works.
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new #guest user
if user.role? == :highered_admin
can :highered_students, HigherEd
end
if user.role? :admin
can :manage, :all
end
end
end
I had it set up for a different role model check.