I am writing some tests for one of my controllers, and all of them are passing except the test for my #create method.
I cannot seem to get this test to pass. I understand that the UnknownFormat error is related to which format I am using (and the respond_to) to post to my controller, but even when I add a json or html format to the post - I still get the error.
Pulling my hair out over this test - would love some guidance on how best to rewrite this test for the best outcome.
The test
test "should create captable" do
sign_in #user
post company_captables_url(#company, #captable), params: { captable: { company_id: #captable.company_id, name: "Great captable!", version: #captable.version } }, as: :json
assert_response :success
end
The method from the controller:
def create
#captable = #company.captables.new(captable_params)
respond_to do |format|
if #captable.save
format.html { redirect_to [#company, #captable], notice: 'Captable was successfully created.' }
format.json { render :show, status: :created, location: [#company, #captable] }
else
format.html { render :new }
format.json { render json: #captable.errors, status: :unprocessable_entity }
end
end
end
The full backtrace on the error :
Error:
CaptablesControllerTest#test_should_create_captable:
ActionController::UnknownFormat: ActionController::UnknownFormat
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/mime_responds.rb:205:in `respond_to'
/Users/jamespember/calmcap/app/controllers/captables_controller.rb:24:in `create'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/abstract_controller/base.rb:194:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/rendering.rb:30:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/abstract_controller/callbacks.rb:42:in `block in process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/callbacks.rb:132:in `run_callbacks'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/abstract_controller/callbacks.rb:41:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/rescue.rb:22:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:168:in `block in instrument'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:168:in `instrument'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/railties/controller_runtime.rb:24:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/abstract_controller/base.rb:134:in `process'
/usr/local/lib/ruby/gems/2.5.0/gems/actionview-5.2.1/lib/action_view/rendering.rb:32:in `process'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal.rb:191:in `dispatch'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal.rb:252:in `dispatch'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:34:in `serve'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/journey/router.rb:52:in `block in serve'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/journey/router.rb:35:in `each'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/journey/router.rb:35:in `serve'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:840:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/warden-1.2.7/lib/warden/manager.rb:36:in `block in call'
/usr/local/lib/ruby/gems/2.5.0/gems/warden-1.2.7/lib/warden/manager.rb:35:in `catch'
/usr/local/lib/ruby/gems/2.5.0/gems/warden-1.2.7/lib/warden/manager.rb:35:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/tempfile_reaper.rb:15:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/etag.rb:25:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/conditional_get.rb:38:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/head.rb:12:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/http/content_security_policy.rb:18:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:232:in `context'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:226:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/cookies.rb:670:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/callbacks.rb:98:in `run_callbacks'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:38:in `call_app'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:26:in `block in call'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:71:in `block in tagged'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:28:in `tagged'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:71:in `tagged'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:26:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/request_id.rb:27:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/executor.rb:14:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/static.rb:127:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/engine.rb:524:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-test-1.1.0/lib/rack/test.rb:119:in `request'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/testing/integration.rb:263:in `process'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/testing/integration.rb:24:in `post'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/testing/integration.rb:350:in `block (2 levels) in <module:Runner>'
/Users/jamespember/calmcap/test/controllers/captables_controller_test.rb:39:in `block in <class:CaptablesControllerTest>'
You should change the test, because you're creating a new Captable entity. You don't need #captable variable here. And you don't need company_id inside the params - it is already in the url.
test "should create captable" do
sign_in #user
assert_difference '#company.captables.count', 1 do # it is good idea to check it was really created
post company_captables_url(#company), params: { captable: { name: "Great captable!", version: 'version_name' } }, as: :json
end
assert_redirected_to company_captable_url(#company, Captable.last)
end
Related
I am trying to customize my 404/422/500 error pages and for some reason I get a blank page with a "500 Internal Server Error". However, in another project the code below works perfectly fine. I hope someone can help me out.
Here are my codes :
Application.rb
module Realbeez
class Application < Rails::Application
config.generators do |generate|
generate.assets false
generate.helper false
generate.test_framework :test_unit, fixture: false
# For error message customizing
config.exceptions_app = self.routes
end
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
end
end
My routes:
get '/404', to: "errors#not_found"
get '/422', to: "errors#unacceptable"
get '/500', to: "errors#internal_error"
My controller ErrorsController:
class ErrorsController < ApplicationController
def not_found
respond_to do |format|
format.html { render status: 404 }
end
end
def unacceptable
respond_to do |format|
format.html { render status: 422 }
end
end
def internal_error
respond_to do |format|
format.html { render status: 500 }
end
end
end
I get the following message in the rails server maybe it can help :
Error during failsafe response: ErrorsController
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/pundit-2.1.0/lib/pundit.rb:192:in `verify_authorized'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:426:in `block in make_lambda'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:236:in `block in halting_and_conditional'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:517:in `block in invoke_after'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:517:in `each'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:517:in `invoke_after'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:133:in `run_callbacks'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/abstract_controller/callbacks.rb:41:in `process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/rescue.rb:22:in `process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications.rb:168:in `block in instrument'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications.rb:168:in `instrument'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/railties/controller_runtime.rb:24:in `process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/abstract_controller/base.rb:134:in `process'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionview-5.2.3/lib/action_view/rendering.rb:32:in `process'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal.rb:191:in `dispatch'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal.rb:252:in `dispatch'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/routing/route_set.rb:34:in `serve'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/router.rb:52:in `block in serve'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/router.rb:35:in `each'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/router.rb:35:in `serve'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/routing/route_set.rb:840:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/show_exceptions.rb:51:in `render_exception'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/show_exceptions.rb:36:in `rescue in call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/rack/logger.rb:38:in `call_app'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/rack/logger.rb:26:in `block in call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/tagged_logging.rb:71:in `block in tagged'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/tagged_logging.rb:28:in `tagged'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/tagged_logging.rb:71:in `tagged'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/rack/logger.rb:26:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/quiet_assets.rb:13:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/request_id.rb:27:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.7/lib/rack/runtime.rb:22:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/executor.rb:14:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/static.rb:127:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/webpacker-4.0.7/lib/webpacker/dev_server_proxy.rb:29:in `perform_request'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-proxy-0.6.5/lib/rack/proxy.rb:57:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:524:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.1.0/lib/puma/configuration.rb:228:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.1.0/lib/puma/server.rb:664:in `handle_request'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.1.0/lib/puma/server.rb:467:in `process_client'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.1.0/lib/puma/server.rb:328:in `block in run'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.1.0/lib/puma/thread_pool.rb:135:in `block in spawn_thread'
Thanks
You can just customize the html error pages in the "public" folder. Did you try it ?
I have a Ruby On Rails weblog application and am getting a NoMethodError in PostsController#show issue when opening a post. PostsController is stored under rails/weblog/app/controllers
I get undefined method 'set_current_page' for #<PostsController:0x007fb162831e70> however set_current_page is defined like so:
private
def set_current_page
#current_page = params[:page] || 1
end
end
and then used in a before_action
before_action :set_current_page, except: [:index]
before_action :set_post, only: [:show, :edit, :update, :destroy]
Full stack trace:
activesupport (5.0.0.1) lib/active_support/callbacks.rb:382:in `block in make_lambda'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:150:in `block (2 levels) in halting_and_conditional'
actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:12:in `block (2 levels) in <module:Callbacks>'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:454:in `block in call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:454:in `each'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:454:in `call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0.1) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.0.1) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.0.1) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0.1) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:725:in `call'
rack (2.0.1) lib/rack/etag.rb:25:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
rack (2.0.1) lib/rack/session/abstract/id.rb:222:in `context'
rack (2.0.1) lib/rack/session/abstract/id.rb:216:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.0.0.1) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
web-console (3.4.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.4.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.4.0) lib/web_console/middleware.rb:18:in `catch'
web-console (3.4.0) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/method_override.rb:22:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
railties (5.0.0.1) lib/rails/engine.rb:522:in `call'
puma (3.6.0) lib/puma/configuration.rb:225:in `call'
puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
puma (3.6.0) lib/puma/server.rb:415:in `process_client'
puma (3.6.0) lib/puma/server.rb:275:in `block in run'
puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'
The posts_controller.rb file:
class PostsController < ApplicationController
before_action :set_current_page, except: [:index]
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.paginate(page: params[:page],
per_page: params[:per_page])
.order('title, user_id')
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
#post.user = User.find_by email: params[:email]
respond_to do |format|
if #post.save
format.html { redirect_to (post_url(#post, page: #current_page)), notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
#post.user = User.find_by email: params[:email]
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to (post_url(#post, page: #current_page)), notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
send
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url(page: #current_page), notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
def set_current_page
#current_page = params[:page] || 1
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body, :page)
end
end
end
You have a typo in your update action - send instead of end.
This means Ruby is interpreting everything after it as part of that update method - and it is not defined outside the method.
In combination with the extra end that's been included after your list of private methods this means you have the correct number of ends so don't see a syntax error, but the class is not interpreted in the way you expect.
I am trying to include a resource into my routes file that shows the addresses of a customer via the url customers/1/addresses.
This is constructed with the following routes code:
resources :customers do
resources :addresses
end
However when I access the URL customers/1/addresses I'm thrown an exception
Couldn't find Customer with 'id'=
This error is caused by the following:
def set_customer
#customer = Customer.find(params[:id])
end
Now, this is because before the :show action is run on the customers controller it's calling the set_customer action.
How to only run the set_customer on the /customer/:customer_id: route?
And exclude set_customer on the /customer/:customer_id:/addresses/ route?
EDIT As Requested the Full Stack Trace
activerecord (4.2.1) lib/active_record/core.rb:155:in `find'
app/controllers/customers_controller.rb:67:in `set_customer'
activesupport (4.2.1) lib/active_support/callbacks.rb:432:in `block in make_lambda'
activesupport (4.2.1) lib/active_support/callbacks.rb:145:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:145:in `block in halting_and_conditional'
activesupport (4.2.1) lib/active_support/callbacks.rb:504:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:504:in `block in call'
activesupport (4.2.1) lib/active_support/callbacks.rb:504:in `each'
activesupport (4.2.1) lib/active_support/callbacks.rb:504:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.1) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.1) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.1) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in `call'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:819:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in `call'
activerecord (4.2.1) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.1) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_call_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.1.3) lib/web_console/middleware.rb:37:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.1) lib/rails/engine.rb:518:in `call'
railties (4.2.1) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/Users/xavier/.rbenv/versions/2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/xavier/.rbenv/versions/2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/xavier/.rbenv/versions/2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
EDIT customers_controller.rb
class CustomersController < ApplicationController
before_action :set_customer, only: [:show, :edit, :update, :destroy]
# GET /customers
# GET /customers.json
def index
#customers = Customer.all
end
# GET /customers/1
# GET /customers/1.json
def show
end
# GET /customers/new
def new
#customer = Customer.new
end
# GET /customers/1/edit
def edit
end
# POST /customers
# POST /customers.json
def create
#customer = Customer.new(customer_params)
respond_to do |format|
if #customer.save
format.html { redirect_to #customer, notice: 'Customer was successfully created.' }
format.json { render :show, status: :created, location: #customer }
else
format.html { render :new }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /customers/1
# PATCH/PUT /customers/1.json
def update
respond_to do |format|
if #customer.update(customer_params)
format.html { redirect_to #customer, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: #customer }
else
format.html { render :edit }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /customers/1
# DELETE /customers/1.json
def destroy
#customer.destroy
respond_to do |format|
format.html { redirect_to customers_url, notice: 'Customer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_customer
#customer = Customer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def customer_params
params.require(:customer).permit(:first_name, :middle_name, :last_name, :cyrillic_first_name, :cyrillic_middle_name, :cyrillic_last_name, :email_address, :hashed_password, allergen_ids: [])
end
end
Edit Addresses Controller
class AddressesController < ApplicationController
before_action :set_address, only: [:show, :edit, :update, :destroy]
# GET /addresses
# GET /addresses.json
def index
#addresses = Address.all
end
# GET /addresses/1
# GET /addresses/1.json
def show
end
# GET /addresses/new
def new
#address = Address.new
end
# GET /addresses/1/edit
def edit
end
# POST /addresses
# POST /addresses.json
def create
#address = Address.new(address_params)
respond_to do |format|
if #address.save
format.html { redirect_to #address, notice: 'Address was successfully created.' }
format.json { render :show, status: :created, location: #address }
else
format.html { render :new }
format.json { render json: #address.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /addresses/1
# PATCH/PUT /addresses/1.json
def update
respond_to do |format|
if #address.update(address_params)
format.html { redirect_to #address, notice: 'Address was successfully updated.' }
format.json { render :show, status: :ok, location: #address }
else
format.html { render :edit }
format.json { render json: #address.errors, status: :unprocessable_entity }
end
end
end
# DELETE /addresses/1
# DELETE /addresses/1.json
def destroy
#address.destroy
respond_to do |format|
format.html { redirect_to addresses_url, notice: 'Address was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_address
#address = Address.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def address_params
params[:address]
end
end
When you access a /customers/:id path, set_customer can do its job with params[:id], but at /customers/:customer_id/addresses, any :id is the Address ID. I'm not sure why set_customer is running at all given the code you've posted so far, but updating it to
Customer.find(params[:customer_id] || params[:id])
will let it work for both situations.
How to only run the set_customer on the /customer/:customer_id: route?
/customers/:customer_id: How's it possible? You are only gonna get customer_id if it is followed by addresses/:id like:
/customers/:customer_id/addresses/
/customers/:customer_id/addresses/new
/customers/:customer_id/addresses/:id/edit
And that actually corresponds the the show action in AddressesController.
And exclude set_customer on the /customer/:customer_id:/addresses/ route?
/customers/:customer_id/:addresses corresponds to index action in AddressesController, and there you can exclude set_customer for index action.
I am a noob. After an exhaustive search of other questions, I was unable to find similar posts with clear and correct answers.
First, let me explain that my app has nested associations as follows:
User has one customer
Customer has many accounts
User has many accounts through customer
So far, I've created a user with Devise, and then manually assigned her to a customer object in the console. Also created a corresponding Account in the console. (Haven't worked out the form yet for the nested creation).
All I want to do right now is bring the user, once logged in, to a "landing" page which shows her a listing of her accounts. It sounds pretty simple, but I get the following error:
ActionController::UnknownFormat in AccountsController#index
The code referenced by the error...
#accounts = #user.accounts
respond_to do |format|
format.html # index.html.erb
format.json { render json: #account }
end
Here is the whole method in the Accounts Controller...
def index
#user = current_user
#accounts = #user.accounts
respond_to do |format|
format.html # index.html.erb
format.json { render json: #account }
end
end
I don't understand what's causing the error. Here is the view code for the Accounts index...
<h1>Listing accounts</h1>
<table id="indexTable" class="table table-striped">
<thead>
<tr>
<th colspan="5"></th>
</tr>
</thead>
<tbody>
<% if Account.exists? %>
<% for account in #accounts do %>
<tr>
<td><%= account.id %></td>
<td><%= %></td>
<td><%= link_to 'Show', account %></td>
<td><%= link_to 'Edit', edit_account_path(account) %></td>
<td><%= link_to 'Destroy', account, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% else %>
<div class="jumbotron" style="text-align: center; border: 1px solid #DDD; border-radius: 8px;">
<h1>No accounts to display</h1>
</div>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Account', new_account_path %>
I admit, the view itself may have its own issues. But it's not even getting to the view. The error points to the controller, but could it be a problem with routing? This is what routes.rb looks like...
Rails.application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
devise_scope :user do
post "/accounts/adminview" => "devise/sessions#new"
end
...
root 'home#index'
...
resources :accounts do
member do
get :adminview
end
end
resources :account_types, :accounts, :administrators, :customers, :transaction_types, :transactions, :users
...
end
It seems like there should be a simple solution for this, but I am a noob. Can anyone help me understand what to do? How do I get rid of this error and take my user to a listing of her accounts?
Thank you
I'm using Rails 4.1.8
UPDATE
As requested, here is the whole stack trace...
actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:440:in `retrieve_collector_from_mimes'
actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:256:in `respond_to'
app/controllers/accounts_controller.rb:11:in `index'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
ctionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
puma (2.11.1) lib/puma/server.rb:507:in `handle_request'
puma (2.11.1) lib/puma/server.rb:375:in `process_client'
puma (2.11.1) lib/puma/server.rb:262:in `block in run'
puma (2.11.1) lib/puma/thread_pool.rb:104:in `call'
puma (2.11.1) lib/puma/thread_pool.rb:104:in `block in spawn_thread'
UPDATE
As requested, the full Accounts controller...
class AccountsController < ApplicationController
before_filter :authenticate_user!
before_action :set_account, only: [:show, :edit, :update, :destroy]
# GET /accounts
# GET /accounts.json
def index
#user = current_user
#accounts = #user.accounts
respond_to do |format|
format.html # index.html.erb
format.json { render json: #accounts }
end
end
def adminview
#accounts = Account.all
respond_to do |format|
format.html # adminview.html.erb
format.json { render json: #accounts }
end
end
# GET /accounts/1
# GET /accounts/1.json
def show
end
# GET /accounts/new
def new
#user = User.find(params[:user_id])
#customer = #user.customer.build
#account = #customer.accounts.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #account }
end
end
# GET /accounts/1/edit
def edit
end
# POST /accounts
# POST /accounts.json
def create
#account = Account.new(account_params)
respond_to do |format|
if #account.save
format.html { redirect_to #account, notice: 'Account was successfully created.' }
format.json { render :show, status: :created, location: #account }
else
format.html { render :new }
format.json { render json: #account.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /accounts/1
# PATCH/PUT /accounts/1.json
def update
respond_to do |format|
if #account.update(account_params)
format.html { redirect_to #account, notice: 'Account was successfully updated.' }
format.json { render :show, status: :ok, location: #account }
else
format.html { render :edit }
format.json { render json: #account.errors, status: :unprocessable_entity }
end
end
end
# DELETE /accounts/1
# DELETE /accounts/1.json
def destroy
#account.destroy
respond_to do |format|
format.html { redirect_to accounts_url, notice: 'Account was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_account
#account = Account.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def account_params
params[:account]
end
end
UPDATE
As requested, the logs just before the error (starting at sign in)...
Started GET "/users/sign_in" for 127.0.0.1 at 2015-03-07 12:28:50 -0500
Processing by Devise::SessionsController#new as HTML
Rendered devise/shared/_links.html.erb (0.5ms)
Rendered devise/sessions/new.html.erb within layouts/application (3.5ms)
Rendered application/_navigation.html.erb (0.5ms)
Completed 200 OK in 166ms (Views: 163.0ms | ActiveRecord: 0.0ms)
Started POST "/users/sign_in" for 127.0.0.1 at 2015-03-07 12:28:54 -0500
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7Eoj56l0rRcheddj2tSE9sSm+5wXi7/bxVVFsPga+XM=", "user"=>{"login"=>"charlie", "password"=>"[FILTERED]"}, "commit"=>"Log in"}
User Load (0.9ms) SELECT `users`.* FROM `users` WHERE (lower(username) = 'charlie' OR lower(email) = 'charlie') ORDER BY `users`.`id` ASC LIMIT 1
(0.2ms) BEGIN
SQL (0.5ms) UPDATE `users` SET `current_sign_in_at` = '2015-03-07 17:28:54', `last_sign_in_at` = '2015-03-07 17:27:42', `sign_in_count` = 17 WHERE `users`.`id` = x'ac045b738f9c446f9cc2d86b01cea3b7'
(94.7ms) COMMIT
Redirected to http://localhost:3000/accounts.ac045b73-8f9c-446f-9cc2-d86b01cea3b7
Completed 302 Found in 187ms (ActiveRecord: 96.2ms)
Started GET "/accounts.ac045b73-8f9c-446f-9cc2-d86b01cea3b7" for 127.0.0.1 at 2015-03-07 12:28:54 -0500
Processing by AccountsController#index as
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = x'ac045b738f9c446f9cc2d86b01cea3b7' ORDER BY `users`.`id` ASC LIMIT 1
Completed 406 Not Acceptable in 6ms
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/accounts_controller.rb:11:in `index'
Rendered /home/barnabas/.rvm/gems/ruby-2.1.5/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.9ms)
Rendered /home/barnabas/.rvm/gems/ruby-2.1.5/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.4ms)
Rendered /home/barnabas/.rvm/gems/ruby-2.1.5/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
Rendered /home/barnabas/.rvm/gems/ruby-2.1.5/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (14.3ms)
For /accounts.ac045b73-8f9c-446f-9cc2-d86b01cea3b7 your app is taking as format ac045b73-8f9c-446f-9cc2-d86b01cea3b7 but your controller only knows how to handle html and json.
You should verify why /users/sign_in is redirecting to that weird link
The only error I can see is that in your controller, this:
format.json { render json: #account }
should be
format.json { render json: #accounts }
Notice the extra 's' on #accounts. But that may not be all of it. If it is, great! If not can yo post your full Accounts controller and stack trace so we can work through this?
I have a large number of mostly static pages which I've put into a database. I'm trying to retrieve these and dynamically generate a URL (similar to this: http://railscasts.com/episodes/117-semi-static-pages-revised. My model:
class Page < ActiveRecord::Base
validates_uniqueness_of :permalink
def to_param
permalink
end
end
Pages Controller:
class PagesController < ApplicationController
before_action :set_page, only: [:show, :edit, :update, :destroy]
# GET /pages
# GET /pages.json
def index
#pages = Page.all
end
# GET /pages/1
# GET /pages/1.json
def show
#page = Page.find_by_permalink!(params[:id])
end
# GET /pages/new
def new
#page = Page.new
end
# GET /pages/1/edit
def edit
#page = Page.find_by_permalink!(params[:id])
end
# POST /pages
# POST /pages.json
def create
#page = Page.new(page_params)
respond_to do |format|
if #page.save
format.html { redirect_to #page, notice: 'Page was successfully created.' }
format.json { render :show, status: :created, location: #page }
else
format.html { render :new }
format.json { render json: #page.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /pages/1
# PATCH/PUT /pages/1.json
def update
respond_to do |format|
if #page.update(page_params)
format.html { redirect_to #page, notice: 'Page was successfully updated.' }
format.json { render :show, status: :ok, location: #page }
else
format.html { render :edit }
format.json { render json: #page.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pages/1
# DELETE /pages/1.json
def destroy
#page.destroy
respond_to do |format|
format.html { redirect_to pages_url, notice: 'Page was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_page
#page = Page.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def page_params
params.require(:page).permit(:name, :permalink, :content)
end
end
Routes:
resources :pages, except: :show
resources :products
root to: 'products#index'
get ':id', to: 'pages#show', as: :page
I know there have been some updates on the way that rails generates scaffolds but I just can;t work out what needs to change to make this work? I'm getting errors thrown up both the routes, but was previously getting an error with #page = Page.find_by_permalink!(params[:id])as well. It was saying that the method was missing but I couldn't see how. I also know I could use something like refinerycms but I really want to try and learn this stuff so any help much appreciated!
ERRORS FROM ROUTES:
Invalid route name, already in use: 'page' You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
FULL TRACE:
ctionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:430:in `add_route'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:1489:in `add_route'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:1466:in `decomposed_match'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:1447:in `block in match'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:1437:in `each'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:1437:in `match'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:630:in `map_method'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:591:in `get'
config/routes.rb:5:in `block in <top (required)>'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:337:in `instance_exec'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:337:in `eval_block'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:315:in `draw'
config/routes.rb:1:in `<top (required)>'
activesupport (4.1.5) lib/active_support/dependencies.rb:241:in `load'
activesupport (4.1.5) lib/active_support/dependencies.rb:241:in `block in load'
activesupport (4.1.5) lib/active_support/dependencies.rb:232:in `load_dependency'
activesupport (4.1.5) lib/active_support/dependencies.rb:241:in `load'
railties (4.1.5) lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
railties (4.1.5) lib/rails/application/routes_reloader.rb:40:in `each'
railties (4.1.5) lib/rails/application/routes_reloader.rb:40:in `load_paths'
railties (4.1.5) lib/rails/application/routes_reloader.rb:16:in `reload!'
railties (4.1.5) lib/rails/application/routes_reloader.rb:26:in `block in updater'
activesupport (4.1.5) lib/active_support/file_update_checker.rb:75:in `call'
activesupport (4.1.5) lib/active_support/file_update_checker.rb:75:in `execute'
railties (4.1.5) lib/rails/application/routes_reloader.rb:7:in `execute'
railties (4.1.5) lib/rails/application/finisher.rb:83:in `block (2 levels) in <module:Finisher>'
activesupport (4.1.5) lib/active_support/callbacks.rb:438:in `instance_exec'
activesupport (4.1.5) lib/active_support/callbacks.rb:438:in `block in make_lambda'
activesupport (4.1.5) lib/active_support/callbacks.rb:184:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:184:in `block in simple'
activesupport (4.1.5) lib/active_support/callbacks.rb:185:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:185:in `block in simple'
activesupport (4.1.5) lib/active_support/callbacks.rb:185:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:185:in `block in simple'
activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:83:in `prepare!'
actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:71:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.5) lib/rails/engine.rb:514:in `call'
railties (4.1.5) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
This error occurred while loading the following files:
/home/ubuntu/workspace/traditional_cleaning/config/routes.rb