I am following this Rolify Tutorial using devise, cancan, and rolify in a Rails project. After creating a new migration (that works fine), I'm getting this error:
undefined method `to_sym' for nil:NilClass
I don't know what I'm doing bad. This is my debug.
My migration for alter the users table is:
class AlterUsersTable1 < ActiveRecord::Migration
def up
change_table :users, :id => true do |t|
t.references :genre, :null => false, :default => 1
t.string :first_name, :null => false, :default => ''
t.string :last_name, :null => false, :default => ''
t.string :company, :null => true
t.boolean :status, :null => false, :default => true
end
execute <<-SQL
ALTER TABLE users
ADD CONSTRAINT fk_users_genre
FOREIGN KEY (genre_id)
REFERENCES genres(id)
SQL
execute <<-SQL
ALTER TABLE users
DROP PRIMARY KEY, ADD PRIMARY KEY (id, genre_id)
SQL
end
# RESET THE TABLE
def down
execute <<-SQL
ALTER TABLE users
DROP FOREIGN KEY fk_users_genre
SQL
execute <<-SQL
ALTER TABLE users
DROP PRIMARY KEY, ADD PRIMARY KEY (id)
SQL
remove_column :users, :first_name
remove_column :users, :last_name
remove_column :users, :company
remove_column :users, :status
remove_column :users, :genre_id
end
end
My routes file is:
devise_for :users
resources :users
resources :categories
root :to => 'home#index'
The application_controller.rb
before_filter :authenticate_user!
This my trace
activerecord (3.2.19) lib/active_record/sanitization.rb:60:in `block in expand_hash_conditions_for_aggregates'
activerecord (3.2.19) lib/active_record/sanitization.rb:59:in `each'
activerecord (3.2.19) lib/active_record/sanitization.rb:59:in `expand_hash_conditions_for_aggregates'
activerecord (3.2.19) lib/active_record/relation/query_methods.rb:326:in `build_where'
activerecord (3.2.19) lib/active_record/relation/query_methods.rb:136:in `where'
activerecord (3.2.19) lib/active_record/querying.rb:9:in `where'
orm_adapter (0.5.0) lib/orm_adapter/adapters/active_record.rb:17:in `get'
devise (3.2.2) lib/devise/models/authenticatable.rb:208:in `serialize_from_session'
devise (3.2.2) lib/devise.rb:462:in `block (2 levels) in configure_warden!'
warden (1.2.3) lib/warden/session_serializer.rb:34:in `fetch'
warden (1.2.3) lib/warden/proxy.rb:212:in `user'
warden (1.2.3) lib/warden/proxy.rb:318:in `_perform_authentication'
warden (1.2.3) lib/warden/proxy.rb:104:in `authenticate'
warden (1.2.3) lib/warden/proxy.rb:114:in `authenticate?'
devise (3.2.2) app/controllers/devise_controller.rb:103:in `require_no_authentication'
activesupport (3.2.19) lib/active_support/callbacks.rb:407:in `_run__584588561542237872__process_action__1933424417154408597__callbacks'
activesupport (3.2.19) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.19) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.19) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.19) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.19) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.19) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.19) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.19) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.19) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.19) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.19) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.19) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.19) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.19) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.19) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.19) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.19) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.19) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.19) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.19) lib/action_dispatch/routing/route_set.rb:36:in `call'
actionpack (3.2.19) lib/action_dispatch/routing/mapper.rb:43: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.19) lib/action_dispatch/routing/route_set.rb:608: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'
actionpack (3.2.19) 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:25:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.19) 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.19) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.19) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.19) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.19) lib/active_support/callbacks.rb:405:in `_run__3633975209017476260__call__1066395091297338092__callbacks'
activesupport (3.2.19) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.19) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.19) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.19) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.19) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.19) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.19) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.19) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.19) 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.19) 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.19) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.19) lib/rails/engine.rb:484:in `call'
railties (3.2.19) lib/rails/application.rb:231:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.19) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/Users/oscar/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/oscar/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/oscar/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
OK, I don't know why, But removing the custom SQL and call this methods now everything works, But without foreign keys :(
add_column :users, :genre_id, :integer, :default => 1, :null => false
add_index :users, :genre_id
Related
I've been trying to learn ruby(2.6.3) on rails(6.0.1) by following a project to make an Instagram clone.
When I try to create a new post I get "no implicit conversion of String into Integer app/controllers/posts_controller.rb:8:in `create' ". I'm confused because I do not see a string to int so i'm wondering if it is something i'm missing with ruby. I've tried updating my gem versions and also tried adding the "strong_parameters" gem but that did not seem to fix my problem. Thank you for any insight as to what i'm doing wrong
Post_controller.rb
class PostsController < ApplicationController
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
if #post.save
redirect_to dashboard_path, flash: {success: "Post was created successfully!"}
else
redirect_to new_post_path, flash: {danger: "Post failed to save!"}
end
end
def show
end
private
def post_params
params.require(:post).permit(:image, :image_cache)
end
end
Posts table migrate
class CreatePosts < ActiveRecord::Migration[6.0]
def change
create_table :posts do |t|
t.string :image
t.boolean :active
t.references :account
t.timestamps
end
end
end
Post Model
class Post < ApplicationRecord
mount_uploader :image, ImageUploader
end
Post table schema
create_table "posts", force: :cascade do |t|
t.string "image"
t.boolean "active"
t.bigint "account_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["account_id"], name: "index_posts_on_account_id"
end
Full stack trace
carrierwave (2.0.2) lib/carrierwave/sanitized_file.rb:312:in `chmod'
carrierwave (2.0.2) lib/carrierwave/sanitized_file.rb:312:in `chmod!'
carrierwave (2.0.2) lib/carrierwave/sanitized_file.rb:220:in `copy_to'
carrierwave (2.0.2) lib/carrierwave/uploader/cache.rb:141:in `cache!'
carrierwave (2.0.2) lib/carrierwave/mounter.rb:63:in `block (2 levels) in cache'
carrierwave (2.0.2) lib/carrierwave/mounter.rb:176:in `handle_error'
carrierwave (2.0.2) lib/carrierwave/mounter.rb:47:in `block in cache'
carrierwave (2.0.2) lib/carrierwave/mounter.rb:46:in `map'
carrierwave (2.0.2) lib/carrierwave/mounter.rb:46:in `cache'
carrierwave (2.0.2) lib/carrierwave/mount.rb:146:in `image='
carrierwave (2.0.2) lib/carrierwave/mount.rb:373:in `image='
carrierwave (2.0.2) lib/carrierwave/orm/activerecord.rb:75:in `image='
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:51:in `public_send'
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:51:in `_assign_attribute'
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:44:in `block in _assign_attributes'
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:43:in `each'
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:43:in `_assign_attributes'
activerecord (6.0.1) lib/active_record/attribute_assignment.rb:22:in `_assign_attributes'
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:35:in `assign_attributes'
activerecord (6.0.1) lib/active_record/core.rb:326:in `initialize'
activerecord (6.0.1) lib/active_record/inheritance.rb:70:in `new'
activerecord (6.0.1) lib/active_record/inheritance.rb:70:in `new'
app/controllers/posts_controller.rb:8:in `create'
actionpack (6.0.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.0.1) lib/abstract_controller/base.rb:196:in `process_action'
actionpack (6.0.1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.0.1) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.0.1) lib/active_support/callbacks.rb:135:in `run_callbacks'
actionpack (6.0.1) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.0.1) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.0.1) lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
activesupport (6.0.1) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.1) lib/active_support/notifications.rb:180:in `instrument'
actionpack (6.0.1) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (6.0.1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (6.0.1) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.0.1) lib/abstract_controller/base.rb:136:in `process'
actionview (6.0.1) lib/action_view/rendering.rb:39:in `process'
actionpack (6.0.1) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (6.0.1) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (6.0.1) lib/action_dispatch/routing/route_set.rb:51:in `dispatch'
actionpack (6.0.1) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.0.1) lib/action_dispatch/journey/router.rb:49:in `block in serve'
actionpack (6.0.1) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.0.1) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.1) lib/action_dispatch/routing/route_set.rb:837:in `call'
warden (1.2.8) lib/warden/manager.rb:36:in `block in call'
warden (1.2.8) lib/warden/manager.rb:34:in `catch'
warden (1.2.8) lib/warden/manager.rb:34:in `call'
rack (2.0.7) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.0.7) lib/rack/etag.rb:25:in `call'
rack (2.0.7) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.7) lib/rack/head.rb:12:in `call'
actionpack (6.0.1) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.0.7) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.7) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/cookies.rb:648:in `call'
activerecord (6.0.1) lib/active_record/migration.rb:567:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.1) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
web-console (4.0.1) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.0.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `catch'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (6.0.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.1) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.1) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.7) lib/rack/method_override.rb:22:in `call'
rack (2.0.7) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.0.7) lib/rack/sendfile.rb:111:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
webpacker (4.2.0) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.1) lib/rails/engine.rb:526:in `call'
puma (4.3.1) lib/puma/configuration.rb:228:in `call'
puma (4.3.1) lib/puma/server.rb:681:in `handle_request'
puma (4.3.1) lib/puma/server.rb:472:in `process_client'
puma (4.3.1) lib/puma/server.rb:328:in `block in run'
puma (4.3.1) lib/puma/thread_pool.rb:134:in `block in spawn_thread'
Carrierwave config
CarrierWave.configure do |config| config.fog_credentials = { provider: 'AWS', aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], } config.storage = :fog config.permissions = "0666" config.cache_dir = "#{Rails.root}/tmp/" config.fog_directory = ENV['FOG_DIRECTORY'] config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" }end
almost always when I see this, it is passing a string into the index of an array. Sorry to be vague, but I don't see where that would happen in your code.
I get NameError: uninitialized constant Category::Translation when executing:
Category.first.translations
Rails version: 5.1.3
Ruby version: 2.3.3
Models:
# app/models/translation.rb
class Translation < ApplicationRecord
belongs_to :translatable, polymorphic: true
end
# app/models/category.rb
class Category < ApplicationRecord
has_many :translations, :as => :translatable
end
Migration:
class CreateTranslations < ActiveRecord::Migration[5.1]
def change
create_table :translations do |t|
t.string :locale
t.string :key
t.text :value
t.text :interpolations
t.boolean :is_proc, default: false
t.references :translatable, polymorphic: true, index: true
t.timestamps
end
end
end
schema.rb:
create_table "translations", force: :cascade do |t|
t.string "locale"
t.string "key"
t.text "value"
t.text "interpolations"
t.boolean "is_proc", default: false
t.string "translatable_type"
t.integer "translatable_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["translatable_type", "translatable_id"], name: "index_translations_on_translatable_type_and_translatable_id"
end
Full trace:
activerecord (5.1.3) lib/active_record/inheritance.rb:166:in `compute_type'
activerecord (5.1.3) lib/active_record/reflection.rb:401:in `compute_class'
activerecord (5.1.3) lib/active_record/reflection.rb:397:in `klass'
activerecord (5.1.3) lib/active_record/associations/association.rb:127:in `klass'
activerecord (5.1.3) lib/active_record/associations/collection_association.rb:33:in `reader'
activerecord (5.1.3) lib/active_record/associations/builder/association.rb:111:in `translations'
app/controllers/categories_controller.rb:27:in `new'
actionpack (5.1.3) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.3) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.3) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.3) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.3) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.3) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.3) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.3) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.3) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.3) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.3) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:46:in `block in serve'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:834:in `call'
omniauth (1.6.1) lib/omniauth/strategy.rb:189:in `call!'
omniauth (1.6.1) lib/omniauth/strategy.rb:167:in `call'
omniauth (1.6.1) lib/omniauth/strategy.rb:189:in `call!'
omniauth (1.6.1) lib/omniauth/strategy.rb:167:in `call'
omniauth (1.6.1) lib/omniauth/builder.rb:63:in `call'
rack (2.0.3) lib/rack/etag.rb:25:in `call'
rack (2.0.3) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.3) lib/rack/head.rb:12:in `call'
rack (2.0.3) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.3) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.3) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.3) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.3) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.3) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.3) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.3) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
railties (5.1.3) lib/rails/engine.rb:522:in `call'
puma (3.9.1) lib/puma/configuration.rb:224:in `call'
puma (3.9.1) lib/puma/server.rb:602:in `handle_request'
puma (3.9.1) lib/puma/server.rb:435:in `process_client'
puma (3.9.1) lib/puma/server.rb:299:in `block in run'
puma (3.9.1) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
I read on many sites that this error is caused by misconfiguration in assocations (mostly typos). I also tried to set :classname => 'Translation' in Category class, but that didnt helped too. I was not able to identify the problem for the whole day so I am here to beg for help.
Thanks!
I solved it! This was not related to the models directly. #MrYoshiji pointed me to the right direction.
I use I18n gem and i18n-active_record so I can store translations in database.
In initialization Rails loaded this code:
Translation = I18n::Backend::ActiveRecord::Translation
The Translation model was in real I18n::Backend::ActiveRecord::Translation model. And therefore changes in my Translation model took no effect, because the name was linked to another model..
I changed app/models/translation.rb to this:
class Translation < I18n::Backend::ActiveRecord::Translation
belongs_to :translatable, polymorphic: true
end
and deleted line mentioned above from configuration.
Anyway, thank you so much for your comments!
I'm following Ryanb's polymorphic association video to implement a comment system. However, I'm using nested resources, and would like some help
I mimiced most of the stuff in the video but instead of this:
def load_commentable
resource, id = request.path.split('/')[1, 2]
#commentable = resource.singularize.classify.constantize.find(id)
end
I did this:
def load_commentable
#commentable = params[:commentable].classify.constantize.find(commentable_id)
end
def commentable_id
params[(params[:commentable].singularize + "_id").to_sym]
end
and my routes are as follows:
resources :users do
resources :comments, :defaults => { :commentable => 'user' }
resources :styles do
resources :comments, :defaults => { :commentable => 'style' }
end
end
I'm able to display my comments system correctly, but my issue is when I create a new comment in my styles pages
example: localhost:3000/users/1/styles/11
It gets redirected to: localhost:3000/styles/11/comments
and I get an error NoMethodError in CommentsController#create
undefined method `classify' for nil:NilClass
EDIT for additional information
This is my CommentsController
class CommentsController < ApplicationController
before_filter :get_commentable
def index
#comments = #commentable.comments
end
def new
#comment = #commentable.comments.new
end
def edit
#comment = #commentable.comments.find(params[:id])
end
def update
#comment = #commentable.comments.find(params[:id])
if #comment.update_attributes(params[:comment])
flash[:notice] = "Successfully updated comment."
redirect_to current_user
else
render :action => 'edit'
end
end
def create
#comment = #commentable.comments.new(params[:comment])
#comment.user = current_user
if #comment.save
redirect_to #commentable, notice: "Comment created."
else
render :new
end
end
private
def get_commentable
#commentable = params[:commentable].classify.constantize.find(commentable_id)
end
def commentable_id
params[(params[:commentable].singularize + "_id").to_sym]
end
and full trace is
app/controllers/comments_controller.rb:38:in `get_commentable'
activesupport (3.2.13) lib/active_support/callbacks.rb:418:in `_run__3190436191390106396__process_action__768140268660897649__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.13) 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.13) lib/action_dispatch/routing/route_set.rb:612: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.13) 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.13) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.13) 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.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__3801355732529113779__call__1297539504005695456__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) 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.13) 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.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/Users/andrewliu/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/andrewliu/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/andrewliu/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Is this what you're asking for?
Use this instead.
def get_commentable
klass = [Style].detect { |c| params["#{c.name.underscore}_id"] }
#commentable = klass.find(params["#{klass.name.underscore}_id"])
end
Also you could try breaking up your routes by shallow nesting them. More on this here
resources :users do
resources :comments
resources :styles
end
resources :styles do
resources :comments
end
Your view is not passing in a parameter called params[:commentable] so that becomes nil and that's why the error is arising.
I just went through trying to rename my controller and all of the files associated with it.
However, I'm getting this error:
NameError in UsersController#show
uninitialized constant UsersController::PostComment
Here's the users controller
def show
#user = User.find(params[:id])
#microposts = #user.microposts.without_review.paginate page: params[:page], :per_page => 15
#comment = PostComment.new
impressionist(#user)
end
here's my postcomment.rb
class PostComment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
validates :comment_content, presence: true
validates :user_id, presence: true
validates :micropost_id, presence: true
default_scope order: 'postcomments.created_at ASC'
end
routes.rb has
resources :microposts do
resources :postcomments
and finally the full trace
app/controllers/users_controller.rb:17:in `show'
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:436:in `_run__602137435__process_action__956769240__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.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.3) lib/action_dispatch/routing/route_set.rb:600: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.3) 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:25: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.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.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__788066434__call__637757501__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.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) 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.5) 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.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
If you have a file called postcomment.rb then it will map to the class Postcomment. If you want it called PostComment you will need to name it post_comment.rb.
This is because of how the autoloader works. It has conventions you need to follow or it won't be able to find your models.
Remember everything is case sensitive in Ruby.
I find this really strange, so I'm hopeful someone can help me out. I'm using Devise, and have my user model defined like so:
class User < ActiveRecord::Base
devise :database_authenticatable,
:registerable,
:token_authenticatable,
:recoverable,
:rememberable,
:trackable,
:validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation
attr_accessible :authentication_token
en
When I try to sign up a new user, I get the following error:
ArgumentError
This isn't very helpful, because it doesn't provide any other information.
Here are the parameters:
{"utf8"=>"✓",
"authenticity_token"=>"9VwmXYwBHEvdDxuPmyHu6tjcKeM6QoBtz6Y4ipBTtjQ=",
"user"=>{"email"=>"jeremy#[redacted].ca",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}
Below is the stack trace, hopefully someone else can make sense of it.
bcrypt-ruby (3.0.1) lib/bcrypt.rb:160:in `create'
devise (2.2.3) lib/devise/models/database_authenticatable.rb:110:in `password_digest'
devise (2.2.3) lib/devise/models/database_authenticatable.rb:37:in `password='
activerecord (3.2.12) lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
activerecord (3.2.12) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (3.2.12) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (3.2.12) lib/active_record/base.rb:497:in `initialize'
devise (2.2.3) lib/devise/models/registerable.rb:20:in `new'
devise (2.2.3) lib/devise/models/registerable.rb:20:in `new_with_session'
devise (2.2.3) app/controllers/devise/registrations_controller.rb:87:in `build_resource'
devise (2.2.3) app/controllers/devise/registrations_controller.rb:13:in `create'
actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.12) lib/active_support/callbacks.rb:436:in `_run__2695083164076933069__process_action__522201399773442049__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.12) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.12) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.12) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.12) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.12) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.12) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.12) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:36:in `call'
actionpack (3.2.12) lib/action_dispatch/routing/mapper.rb:42: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.12) lib/action_dispatch/routing/route_set.rb:601: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.12) 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.12) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.12) 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.12) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.12) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.12) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `_run__1001537658589897327__call__4072859572188315975__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.12) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.12) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.12) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.12) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.12) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.12) 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.12) 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.12) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.12) lib/rails/engine.rb:479:in `call'
railties (3.2.12) lib/rails/application.rb:223:in `call'
railties (3.2.12) lib/rails/railtie/configurable.rb:30:in `method_missing'
The problem is that the value of config.stretches in config/initializers/devise.rb is too high (it can't be higher than 31). Change it to a lower value and things will work again.
So how did I figure this out? Here's how:
From the stack trace, you can see that the function that throws the ArgumentError in bcrypt-ruby is:
def create(secret, options = { :cost => BCrypt::Engine::DEFAULT_COST })
raise ArgumentError if options[:cost] > 31
Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(options[:cost]), options[:cost]))
end
Ok, so the problem is that Devise is sending a cost higher than 31 (which is the maximum allowed value). Let's look at the Devise call (again from the stack trace):
def password_digest(password)
::BCrypt::Password.create("#{password}#{self.class.pepper}", :cost => self.class.stretches).to_s
end
And that's how we find the problem. :)