I have a call RiskMatrix model that stores a score as a string
If I build the object using FactoryBot and then save I get :
RuntimeError:
Can't modify frozen hash
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/attribute_set/builder.rb:45:in `[]='
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/attribute_set.rb:39:in `write_from_user'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/attribute_methods/write.rb:74:in `write_attribute_with_type_cast'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/attribute_methods/write.rb:56:in `write_attribute'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/attribute_methods/dirty.rb:97:in `write_attribute'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/attribute_methods.rb:369:in `[]='
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/autosave_association.rb:445:in `save_belongs_to_association'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/autosave_association.rb:201:in `block in add_autosave_association_callbacks'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/autosave_association.rb:157:in `instance_eval'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/autosave_association.rb:157:in `block in define_non_cyclic_method'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activesupport-4.2.11/lib/active_support/callbacks.rb:432:in `block in make_lambda'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activesupport-4.2.11/lib/active_support/callbacks.rb:164:in `block in halting'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activesupport-4.2.11/lib/active_support/callbacks.rb:504:in `block in call'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activesupport-4.2.11/lib/active_support/callbacks.rb:504:in `each'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activesupport-4.2.11/lib/active_support/callbacks.rb:504:in `call'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activesupport-4.2.11/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activesupport-4.2.11/lib/active_support/callbacks.rb:778:in `_run_save_callbacks'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/callbacks.rb:302:in `create_or_update'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/persistence.rb:120:in `save'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/validations.rb:37:in `save'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/attribute_methods/dirty.rb:21:in `save'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/transactions.rb:286:in `block (2 levels) in save'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/transactions.rb:220:in `transaction'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/transactions.rb:286:in `block in save'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/activerecord-4.2.11/lib/active_record/transactions.rb:285:in `save'
# ./spec/models/risk_matrix_spec.rb:19:in `block (4 levels) in <top (required)>'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/vcr-2.9.3/lib/vcr/util/variable_args_block_caller.rb:9:in `call_block'
# /Users/mike/.rvm/gems/ruby-2.4.4/gems/vcr-2.9.3/lib/vcr.rb:182:in `use_cassette'
# ./spec/support/vcr.rb:19:in `block (2 levels) in <top (required)>'
I have no idea why this is happening.
Please help
class RiskMatrix < ActiveRecord::Base
belongs_to :user
after_save :alert_admin, :save_suspicious_ip, if: proc {score.length >= ALERT_THRESHOLD}
private
def alert_admin
[...]
end
def save_suspicious_ip
[...]
end
end
risk_matrix_spec.rb
describe 'after_save' do
context 'score.length > ALERT_THRESHOLD' do
it 'should run alert_admin' do
matrix = build(:risk_matrix, score: 'ABCD')
expect(matrix).to receive(:alert_admin)
matrix.save
end
end
end
Related
I'm new to Rails and RSpec and following some tutorials it seems like the output I'm getting from a failing test is far more verbose than it should be. Whenver I run an rspec test here is the output:
.F
Failures:
1) Users can create new projects when providing invalid attributes
Failure/Error: click_button "Create Project"
ActionView::MissingTemplate:
Missing template projects/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/Users/michael/code/project/ticketee/app/views"
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionview-4.2.4/lib/action_view/path_set.rb:46:in `find'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionview-4.2.4/lib/action_view/lookup_context.rb:121:in `find'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionview-4.2.4/lib/action_view/renderer/abstract_renderer.rb:18:in `find_template'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionview-4.2.4/lib/action_view/renderer/template_renderer.rb:40:in `determine_template'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionview-4.2.4/lib/action_view/renderer/template_renderer.rb:8:in `render'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionview-4.2.4/lib/action_view/renderer/renderer.rb:42:in `render_template'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionview-4.2.4/lib/action_view/renderer/renderer.rb:23:in `render'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionview-4.2.4/lib/action_view/rendering.rb:100:in `_render_template'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/streaming.rb:217:in `_render_template'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionview-4.2.4/lib/action_view/rendering.rb:83:in `render_to_body'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/rendering.rb:32:in `render_to_body'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/renderers.rb:37:in `render_to_body'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/abstract_controller/rendering.rb:25:in `render'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/rendering.rb:16:in `render'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/core_ext/benchmark.rb:12:in `ms'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activerecord-4.2.4/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:43:in `render'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/implicit_render.rb:10:in `default_render'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/implicit_render.rb:5:in `send_action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/abstract_controller/base.rb:198:in `process_action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:117:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:117:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:505:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:505:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in `block in instrument'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in `instrument'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activerecord-4.2.4/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/abstract_controller/base.rb:137:in `process'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionview-4.2.4/lib/action_view/rendering.rb:30:in `process'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal.rb:196:in `dispatch'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_controller/metal.rb:237:in `block in action'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:76:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:45:in `serve'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/journey/router.rb:43:in `block in serve'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/journey/router.rb:30:in `each'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/journey/router.rb:30:in `serve'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:821:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/etag.rb:24:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/conditionalget.rb:38:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/middleware/flash.rb:260:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:225:in `context'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:220:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/middleware/cookies.rb:560:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activerecord-4.2.4/lib/active_record/query_cache.rb:36:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:88:in `__run_callbacks__'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/railties-4.2.4/lib/rails/rack/logger.rb:38:in `call_app'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/railties-4.2.4/lib/rails/rack/logger.rb:20:in `block in call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/tagged_logging.rb:68:in `block in tagged'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/tagged_logging.rb:26:in `tagged'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/tagged_logging.rb:68:in `tagged'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/railties-4.2.4/lib/rails/rack/logger.rb:20:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/middleware/request_id.rb:21:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/activesupport-4.2.4/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.4/lib/action_dispatch/middleware/static.rb:116:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/railties-4.2.4/lib/rails/engine.rb:518:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/railties-4.2.4/lib/rails/application.rb:165:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/urlmap.rb:66:in `block in call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `each'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `call'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-test-0.6.3/lib/rack/mock_session.rb:30:in `request'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-test-0.6.3/lib/rack/test.rb:244:in `process_request'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/rack-test-0.6.3/lib/rack/test.rb:67:in `post'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/rack_test/browser.rb:60:in `process'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/rack_test/browser.rb:35:in `process_and_follow_redirects'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/rack_test/browser.rb:26:in `submit'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/rack_test/form.rb:76:in `submit'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/rack_test/node.rb:61:in `click'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/node/element.rb:134:in `block in click'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/node/base.rb:84:in `synchronize'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/node/element.rb:134:in `click'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/node/actions.rb:39:in `click_button'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/session.rb:698:in `block (2 levels) in <class:Session>'
# /Users/michael/.rvm/gems/ruby-2.1.4/gems/capybara-2.5.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/features/creating_projects_spec.rb:24:in `block (2 levels) in <top (required)>'
Finished in 0.24825 seconds (files took 1.51 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/features/creating_projects_spec.rb:23 # Users can create new projects when providing invalid attributes
I've checked in my .rspec file in my project and --warnings is not listed. Is there another place to check for this? Is this just the default output for a failing test, or is there anyway I can fix this?
Well it looks like you read this thread on SO, so I would check to see if you have --warnings turned on in global configuration file specifically in this location, ~/.rspec
I have researched and seen a similar question (Rspec: Testing nested destroy action) but was unable to get the solution to work for me. In the app I am working on, we have an Articles model and users can Follow Articles.
I have used the acts_as_follower gem to implement this, so Article model has a 'followers' method that returns and array of users following that article.
Here is the relevant code:
follows_controller
class FollowsController < ApplicationController
def create
#article = Article.find(params[:article_id])
current_user.follow(#article)
redirect_to #article
end
def destroy
#article = Article.find(params[:article_id])
current_user.stop_following(#article)
redirect_to #article
end
end
routes.rb
get '/articles/:id/history', to: 'articles#history', as: :articles_history
post '/articles/:id/undo', to: 'articles#undo', as: :undo
resources :articles do
resources :follows, :only => [:create, :destroy]
end
schema.rb
create_table "follows", force: :cascade do |t|
t.integer "followable_id", null: false
t.string "followable_type", null: false
t.integer "follower_id", null: false
t.string "follower_type", null: false
t.boolean "blocked", default: false, null: false
t.datetime "created_at"
t.datetime "updated_at"
end
spec/controllers/follows_controller_spec.rb updated per #Peter's comment below
describe '#DELETE destroy' do
it 'deletes a follow' do
#article = FactoryGirl.create(:article)
follow = FactoryGirl.create(:follow, followable_id: #article.id)
# Also, test if the action really deletes a comment.
expect{delete :destroy, id: follow.id, article_id: #article.id}.
to change{#article.followers.count}.by(-1)
end
end
rake routes:
article_follows POST /articles/:article_id/follows(.:format) follows#create
article_follow DELETE /articles/:article_id/follows/:id(.:format) follows#destroy
Here is the error I get on running the test:
Failures:
1) FollowsController#DELETE destroy deletes a follow
Failure/Error: expect{delete :destroy, id: follow.id, followable_id: #article.id}.
ActionController::UrlGenerationError:
No route matches {:action=>"destroy", :controller=>"follows", :followable_id=>"2", :id=>"2"}
Adding Factory and new error based on #PeterAlfvin's suggestion below:
here is my factory:
FactoryGirl.define do
factory :follow do |f|
f.follower_id 1
f.followable_id 1
f.followable_type "Article"
f.follower_type "User"
end
end
and here is the current error:
1) FollowsController#DELETE destroy deletes a follow
Failure/Error: expect{delete :destroy, id: follow.id, article_id: #article.id}.
NoMethodError:
undefined method `stop_following' for nil:NilClass
# ./app/controllers/follows_controller.rb:10:in `destroy'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:198:in `process_action'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/metal/rendering.rb:10:in `process_action'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:19:in `process_action'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/metal/rescue.rb:29:in `process_action'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `block in instrument'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# ./.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `instrument'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
# ./.bundle/gems/ruby/2.2.0/gems/searchkick-0.8.7/lib/searchkick/logging.rb:107:in `process_action'
# ./.bundle/gems/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:137:in `process'
# ./.bundle/gems/ruby/2.2.0/gems/actionview-4.2.1/lib/action_view/rendering.rb:30:in `process'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:632:in `process'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:65:in `process'
# ./.bundle/gems/ruby/2.2.0/gems/devise-3.4.1/lib/devise/test_helpers.rb:19:in `block in process'
# ./.bundle/gems/ruby/2.2.0/gems/devise-3.4.1/lib/devise/test_helpers.rb:72:in `catch'
# ./.bundle/gems/ruby/2.2.0/gems/devise-3.4.1/lib/devise/test_helpers.rb:72:in `_catch_warden'
# ./.bundle/gems/ruby/2.2.0/gems/devise-3.4.1/lib/devise/test_helpers.rb:19:in `process'
# ./.bundle/gems/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:532:in `delete'
# ./spec/controllers/follows_controller_spec.rb:22:in `block (4 levels) in <top (required)>'
# ./spec/controllers/follows_controller_spec.rb:22:in `block (3 levels) in <top (required)>'
# ./spec/spec_helper.rb:57:in `block (3 levels) in <top (required)>'
# ./.bundle/gems/ruby/2.2.0/gems/database_cleaner-1.3.0/lib/database_cleaner/generic/base.rb:15:in `cleaning'
# ./.bundle/gems/ruby/2.2.0/gems/database_cleaner-1.3.0/lib/database_cleaner/base.rb:92:in `cleaning'
# ./.bundle/gems/ruby/2.2.0/gems/database_cleaner-1.3.0/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
# ./.bundle/gems/ruby/2.2.0/gems/database_cleaner-1.3.0/lib/database_cleaner/configuration.rb:87:in `call'
# ./.bundle/gems/ruby/2.2.0/gems/database_cleaner-1.3.0/lib/database_cleaner/configuration.rb:87:in `cleaning'
# ./spec/spec_helper.rb:56:in `block (2 levels) in <top (required)>'
I think the routing error was indeed due to specifying followable_id rather than article_id as implied by the comment you received. The subsequent no method error, however, was due to making that change in the factory call as well. The following is what you should try:
describe '#DELETE destroy' do
it 'deletes a follow' do
#article = FactoryGirl.create(:article)
follow = FactoryGirl.create(:follow, followable_id: #article.id)
# Test if the action really deletes a follow.
expect{delete :destroy, id: follow.id, article_id: #article.id}.
to change{#article.followers.count}.by(-1)
end
end
After upgrading engine in_quotex to Rails 4.2.0 from Rails 3.2.12, click_button 'Save' in integration test throws out error:
Failure/Error: click_button 'Save'
AbstractController::ActionNotFound:
The action 'submit' could not be found for InQuotex::QuotesController
Here is the spec case:
it "should work for workflow" do
visit in_quotex.quotes_path
click_link 'Submit'
fill_in 'quote_wf_comment', :with => 'this line tests workflow'
fill_in 'quote_tax', :with => '10.00'
click_button 'Save'
...
end
Something really puzzles us is that in debug, InQuotex::QuotesController.method_defined?(:submit) returns true which means there is a method submit defined in quotes controller (the method submit is include in application controller). The same piece of spec code works fine with Rails 3.2.12. I am wondering what could cause this type of strange error and how to fix it.
in_quotex.gemspec:
s.add_dependency "rails", "~>4.2.0"
s.add_dependency "jquery-rails"
s.add_dependency "simple_form"
s.add_dependency "will_paginate"
s.add_dependency "database_cleaner"
s.add_dependency "execjs"
s.add_dependency 'protected_attributes'
s.add_dependency "sass-rails", '~>5.0.1'
s.add_dependency "coffee-rails", '~>4.1.0'
s.add_dependency "uglifier"
s.add_development_dependency "sqlite3"
s.add_development_dependency "rspec-rails", "~> 3.2.0"
s.add_development_dependency "factory_girl_rails", '~>4.5'
s.add_development_dependency 'capybara'
s.add_development_dependency 'launchy' #with capybara
Here is the submit form header:
<%= simple_form_for #workflow_model_object, :method => :patch, :url => eval(#workflow_result_url) do |f| %>
debug value:
#workflow_result_url="submit_quote_path"
#workflow_model_object = {InQuotex::Quote}#<InQuotex::Quote:0x632ac48>
Backtrace output (v.very long!):
The action 'submit' could not be found for InQuotex::QuotesController
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/abstract_controller/base.rb:132:in `process'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionview-4.2.0/lib/action_view/rendering.rb:30:in `process'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_controller/metal.rb:195:in `dispatch'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_controller/metal.rb:236:in `block in action'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:73:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:42:in `serve'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/journey/router.rb:43:in `block in serve'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/journey/router.rb:30:in `each'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/journey/router.rb:30:in `serve'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:802:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/engine.rb:518:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/railtie.rb:194:in `public_send'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/railtie.rb:194:in `method_missing'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/mapper.rb:51:in `serve'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/journey/router.rb:43:in `block in serve'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/journey/router.rb:30:in `each'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/journey/router.rb:30:in `serve'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:802:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/etag.rb:24:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/conditionalget.rb:38:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/head.rb:13:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/flash.rb:260:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/session/abstract/id.rb:225:in `context'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/session/abstract/id.rb:220:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/cookies.rb:560:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/query_cache.rb:36:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `_run_callbacks'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/rack/logger.rb:38:in `call_app'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/rack/logger.rb:20:in `block in call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/tagged_logging.rb:68:in `block in tagged'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/tagged_logging.rb:26:in `tagged'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/tagged_logging.rb:68:in `tagged'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/rack/logger.rb:20:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/request_id.rb:21:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/methodoverride.rb:22:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/runtime.rb:18:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/lock.rb:17:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/static.rb:113:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/sendfile.rb:113:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/engine.rb:518:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/application.rb:164:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/urlmap.rb:66:in `block in call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/urlmap.rb:50:in `each'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.6.0/lib/rack/urlmap.rb:50:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-test-0.6.3/lib/rack/mock_session.rb:30:in `request'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-test-0.6.3/lib/rack/test.rb:244:in `process_request'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rack-test-0.6.3/lib/rack/test.rb:67:in `post'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/rack_test/browser.rb:60:in `process'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/rack_test/browser.rb:35:in `process_and_follow_redirects'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/rack_test/browser.rb:26:in `submit'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/rack_test/form.rb:75:in `submit'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/rack_test/node.rb:60:in `click'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/node/element.rb:133:in `block in click'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/node/base.rb:84:in `synchronize'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/node/element.rb:133:in `click'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/node/actions.rb:37:in `click_button'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/session.rb:676:in `block (2 levels) in <class:Session>'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/features/in_quotex_linke_tests_spec.rb:156:in `block (3 levels) in <top (required)>'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:177:in `instance_exec'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:177:in `block in run'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:385:in `block in with_around_and_singleton_context_hooks'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:343:in `block in with_around_example_hooks'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/hooks.rb:474:in `block in run'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/hooks.rb:614:in `block in run_around_example_hooks_for'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:247:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:247:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-rails-3.2.0/lib/rspec/rails/adapters.rb:111:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:333:in `instance_exec'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:333:in `instance_exec'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/hooks.rb:387:in `execute_with'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/hooks.rb:616:in `block (2 levels) in run_around_example_hooks_for'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:247:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:247:in `call'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/hooks.rb:617:in `run_around_example_hooks_for'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/hooks.rb:474:in `run'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:343:in `with_around_example_hooks'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:385:in `with_around_and_singleton_context_hooks'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:174:in `run'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:548:in `block in run_examples'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:544:in `map'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:544:in `run_examples'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:512:in `run'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:513:in `block in run'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:513:in `map'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:513:in `run'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:110:in `block (3 levels) in run_specs'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:110:in `map'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:110:in `block (2 levels) in run_specs'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/configuration.rb:1526:in `with_suite_hooks'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:109:in `block in run_specs'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/reporter.rb:62:in `report'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:108:in `run_specs'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:86:in `run'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:70:in `run'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:38:in `invoke'
# c:/Ruby200/lib/ruby/gems/2.0.0/gems/rspec-core-3.2.0/exe/rspec:4:in `<top (required)>'
# c:/Ruby200/bin/rspec:23:in `load'
# c:/Ruby200/bin/rspec:23:in `<main>'
I cant seem to get the root to change in my Rails 4 application. I simply want the root to change once a user logs in, so that it points to the user show page.
I get the following error:
Couldn't find User without an ID
My routes.rb looks like so:
devise_for :users, :path_names => {:sign_up => "register", }
authenticated :user do
root to: "users#show", as: :authenticated_root
end
unauthenticated do
get 'welcome/index'
root to: "static#index"
end
match 'users/:id' => 'users#show', as: :user, via: :get
get "/asian-century" => "static#asian-century"
get "/pricing" => "static#about"
get "/about" => "static#pricing"
get "/chatroom" => "static#chatroom"
My users controller likes like so:
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
#users = User.all
end
def show
if params[:id]
#user = User.find(params[:id])
else
#user = current_user
end
#userlessons = #user.lessons_to_attend.where("starts_at > ?", Time.now.advance(:hours => -1)).order(starts_at: :asc)
unless #user == current_user
redirect_to :back, :alert => "Access denied."
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params[:user]
end
end
...
Where am I going wrong here? I have looked at multiple solutions to this problem and I can't seem to find an answer.
EDIT:
Error in terminal:
Started GET "/" for 127.0.0.1 at 2014-07-07 14:03:27 +0800
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Processing by UsersController#show as HTML
Completed 404 Not Found in 1ms
ActiveRecord::RecordNotFound - Couldn't find User without an ID:
activerecord (4.1.1) lib/active_record/relation/finder_methods.rb:402:in `find_with_ids'
activerecord (4.1.1) lib/active_record/relation/finder_methods.rb:68:in `find'
activerecord (4.1.1) lib/active_record/querying.rb:3:in `find'
() Users/Dizzy/Documents/MM/mandarinminds/app/controllers/users_controller.rb:77:in `set_user'
activesupport (4.1.1) lib/active_support/callbacks.rb:424:in `block in make_lambda'
activesupport (4.1.1) lib/active_support/callbacks.rb:143:in `block in halting_and_conditional'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
I'm trying to get my Rails 4.0.2 (ruby 2.1.0) app to use Syslog.
Here is my config:
config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new('myApp'))
I noticed that Rails.application.config.logger corresponds to the logger I set up in my production.rb file. If I use it directly (in rails c for example), I successfully get my messages in syslog.
What confuses me is that Rails.logger (which is, if I understand, the one that rails itself uses) is not the same object. It's a simple Logger like the default development one. As a consequence I get all my log messages from rails in stdout.
Output form rails c:
2.1.0 :004 > Rails.application.config.logger
=> #<Syslog::Logger:0x00000003529fd8 #level=1, #formatter=#<Syslog::Logger::Formatter:0x00000003529fb0>, #facility=8>
2.1.0 :005 > Rails.logger
=> #<Logger:0x000000032b21d8 #progname=nil, #level=1, #default_formatter=#<Logger::Formatter:0x000000032b21b0 #datetime_format=nil>, #formatter=#<ActiveSupport::Logger::SimpleFormatter:0x000000032b1fd0 #datetime_format=nil>, #logdev=#<Logger::LogDevice:0x000000032b20c0 #shift_size=nil, #shift_age=nil, #filename=nil, #dev=#<IO:<STDOUT>>, #mutex=#<Logger::LogDevice::LogDeviceMutex:0x000000032b2070 #mon_owner=nil, #mon_count=0, #mon_mutex=#<Mutex:0x000000032b2020>>>>
Could anyone please tell me if this is an expected behavior ? Am I missing something ?
** EDIT **
I added some traces (value and caller) on Rails' logger= method and here is what I get:
App 18055 stdout: SET LOGGER #<Syslog::Logger:0x000000044b08a8>
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/application/bootstrap.rb:32:in `block in <module:Bootstrap>'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `instance_exec'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `run'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `call'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:54:in `run_initializers'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/application.rb:215:in `initialize!'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
App 18055 stdout: /home/myUser/myApp/releases/20140129114805/config/environment.rb:5:in `<top (required)>'
App 18055 stdout: config.ru:3:in `require'
App 18055 stdout: config.ru:3:in `block in <main>'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
App 18055 stdout: config.ru:1:in `new'
App 18055 stdout: config.ru:1:in `<main>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `eval'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `preload_app'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:158:in `<module:App>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:28:in `<main>'
App 18055 stdout: SET LOGGER #<Logger:0x000000042ea8c0>
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rails_stdout_logging-0.0.3/lib/rails_stdout_logging/rails3.rb:7:in `set_logger'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rails_stdout_logging-0.0.3/lib/rails_stdout_logging/railtie.rb:6:in `block in <class:Railtie>'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:36:in `call'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:44:in `each'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/application/bootstrap.rb:76:in `block in <module:Bootstrap>'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `instance_exec'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `run'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `call'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:54:in `run_initializers'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/application.rb:215:in `initialize!'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
App 18055 stdout: /home/myUser/myApp/releases/20140129114805/config/environment.rb:5:in `<top (required)>'
App 18055 stdout: config.ru:3:in `require'
App 18055 stdout: config.ru:3:in `block in <main>'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
App 18055 stdout: config.ru:1:in `new'
App 18055 stdout: config.ru:1:in `<main>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `eval'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `preload_app'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:158:in `<module:App>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:28:in `<main>'
The first call set the right logger but it is then overwritten for a reason that I don't understand.
Thanks in advance.
Got it!
Answer: Do not use the "rails_12factor" gem if you are not using a 12factor host...
One of its features is to overwrite Rails' logger using "rails_stdout_logging" in "before_initialize".