My ruby on rails app has an endpoint at http://localhost:3000/searches/new
. When I navigate browser there, everything is fine. But when I navigate to http://localhost:3000/searches/new.json
, it raises this error resulting in http-500: SearchesController#new is missing a template for this request format and variant. request.formats: ["application/json"] request.variant: []
How can I make it stop doing that? I want it to return 404 like every other request for resource that doesn’t exit.
An more theoretically, it appears ruby on rails treats the extension at the end of URI’s path as some kind of format specifier. Is that just an Ruby on Rails convention? Or is there some obscure clause of URI or REST specification that I’m not aware of?
I’m using ruby on rails 5.2
It’s strange. Some endpoints return 406 when I append ‘.json’ to the end of path. Others, 500. here is the stack trace of a latter.
I, [2020-06-16T06:10:06.410101 #2816] INFO -- : Processing by WelcomeController#talent as JSON
D, [2020-06-16T06:10:06.426042 #2816] DEBUG -- : User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 81], ["LIMIT", 1]]
D, [2020-06-16T06:10:06.434018 #2816] DEBUG -- : Private::Conversation Load (0.9ms) SELECT "private_conversations".* FROM "private_conversations" WHERE ("private_conversations"."recipient_id" = $1 OR "private_conversations"."sender_id" = $2) [["recipient_id", 81], ["sender_id", 81]]
D, [2020-06-16T06:10:06.435528 #2816] DEBUG -- : Group::Conversation Load (0.7ms) SELECT "group_conversations".* FROM "group_conversations" INNER JOIN "group_conversations_users" ON "group_conversations"."id" = "group_conversations_users"."conversation_id" WHERE "group_conversations_users"."user_id" = $1 [["user_id", 81]]
D, [2020-06-16T06:10:06.437254 #2816] DEBUG -- : (0.6ms) SELECT "group_conversations"."id" FROM "group_conversations" INNER JOIN "group_conversations_users" ON "group_conversations"."id" = "group_conversations_users"."conversation_id" WHERE "group_conversations_users"."user_id" = $1 [["user_id", 81]]
D, [2020-06-16T06:10:06.439379 #2816] DEBUG -- : CACHE (0.1ms) SELECT "group_conversations"."id" FROM "group_conversations" INNER JOIN "group_conversations_users" ON "group_conversations"."id" = "group_conversations_users"."conversation_id" WHERE "group_conversations_users"."user_id" = $1 [["user_id", 81]]
E, [2020-06-16T06:10:06.448074 #2816] ERROR -- : Traceback (most recent call last):
96: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/puma-4.3.3/lib/puma/thread_pool.rb:134:in `block in spawn_thread'
95: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/puma-4.3.3/lib/puma/server.rb:328:in `block in run'
94: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/puma-4.3.3/lib/puma/server.rb:472:in `process_client'
93: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/puma-4.3.3/lib/puma/server.rb:682:in `handle_request'
92: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/puma-4.3.3/lib/puma/configuration.rb:228:in `call'
91: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/railties-5.2.4.2/lib/rails/engine.rb:524:in `call'
90: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-cors-1.1.1/lib/rack/cors.rb:100:in `call'
89: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-mini-profiler-2.0.1/lib/mini_profiler/profiler.rb:312:in `call'
88: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-2.2.2/lib/rack/sendfile.rb:110:in `call'
87: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/static.rb:127:in `call'
86: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/executor.rb:14:in `call'
85: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
84: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-2.2.2/lib/rack/runtime.rb:22:in `call'
83: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-2.2.2/lib/rack/method_override.rb:24:in `call'
82: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/request_id.rb:27:in `call'
81: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/request_store-1.5.0/lib/request_store/middleware.rb:19:in `call'
80: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
79: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/railties-5.2.4.2/lib/rails/rack/logger.rb:26:in `call'
78: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/tagged_logging.rb:71:in `tagged'
77: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/tagged_logging.rb:28:in `tagged'
76: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/tagged_logging.rb:71:in `block in tagged'
75: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/railties-5.2.4.2/lib/rails/rack/logger.rb:26:in `block in call'
74: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/railties-5.2.4.2/lib/rails/rack/logger.rb:38:in `call_app'
73: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
72: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
71: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/better_errors-2.7.0/lib/better_errors/middleware.rb:57:in `call'
70: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/better_errors-2.7.0/lib/better_errors/middleware.rb:79:in `better_errors_call'
69: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/better_errors-2.7.0/lib/better_errors/middleware.rb:84:in `protected_app_call'
68: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/executor.rb:14:in `call'
67: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
66: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/callbacks.rb:98:in `run_callbacks'
65: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
64: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activerecord-5.2.4.2/lib/active_record/migration.rb:559:in `call'
63: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/cookies.rb:670:in `call'
62: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-2.2.2/lib/rack/session/abstract/id.rb:260:in `call'
61: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-2.2.2/lib/rack/session/abstract/id.rb:266:in `context'
60: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/http/content_security_policy.rb:18:in `call'
59: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-2.2.2/lib/rack/head.rb:12:in `call'
58: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-2.2.2/lib/rack/conditional_get.rb:27:in `call'
57: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-2.2.2/lib/rack/etag.rb:27:in `call'
56: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-2.2.2/lib/rack/tempfile_reaper.rb:15:in `call'
55: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/warden-1.2.8/lib/warden/manager.rb:34:in `call'
54: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/warden-1.2.8/lib/warden/manager.rb:34:in `catch'
53: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/warden-1.2.8/lib/warden/manager.rb:36:in `block in call'
52: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/remotipart-1.4.4/lib/remotipart/middleware.rb:32:in `call'
51: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/omniauth-1.9.1/lib/omniauth/strategy.rb:169:in `call'
50: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/omniauth-1.9.1/lib/omniauth/strategy.rb:192:in `call!'
49: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/rack-pjax-1.1.0/lib/rack/pjax.rb:12:in `call'
48: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/routing/route_set.rb:840:in `call'
47: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/journey/router.rb:35:in `serve'
46: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/journey/router.rb:35:in `each'
45: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/journey/router.rb:52:in `block in serve'
44: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/routing/route_set.rb:34:in `serve'
43: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
42: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal.rb:252:in `dispatch'
41: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal.rb:191:in `dispatch'
40: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionview-5.2.4.2/lib/action_view/rendering.rb:32:in `process'
39: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/abstract_controller/base.rb:134:in `process'
38: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activerecord-5.2.4.2/lib/active_record/railties/controller_runtime.rb:24:in `process_action'
37: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
36: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
35: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/notifications.rb:168:in `instrument'
34: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
33: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/notifications.rb:168:in `block in instrument'
32: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
31: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/rescue.rb:22:in `process_action'
30: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/abstract_controller/callbacks.rb:41:in `process_action'
29: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/callbacks.rb:132:in `run_callbacks'
28: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/abstract_controller/callbacks.rb:42:in `block in process_action'
27: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/rendering.rb:30:in `process_action'
26: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/abstract_controller/base.rb:194:in `process_action'
25: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
24: from /ch/app/controllers/welcome_controller.rb:13:in `talent'
23: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/remotipart-1.4.4/lib/remotipart/render_overrides.rb:23:in `render'
22: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/instrumentation.rb:45:in `render'
21: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/sunspot_rails-2.5.0/lib/sunspot/rails/railties/controller_runtime.rb:15:in `cleanup_view_runtime'
20: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activerecord-5.2.4.2/lib/active_record/railties/controller_runtime.rb:31:in `cleanup_view_runtime'
19: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
18: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/instrumentation.rb:46:in `block in render'
17: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/core_ext/benchmark.rb:14:in `ms'
16: from /Users/makedon/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
15: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/activesupport-5.2.4.2/lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
14: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
13: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/rendering.rb:36:in `render'
12: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/abstract_controller/rendering.rb:25:in `render'
11: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/renderers.rb:142:in `render_to_body'
10: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/rendering.rb:52:in `render_to_body'
9: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionview-5.2.4.2/lib/action_view/rendering.rb:84:in `render_to_body'
8: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionpack-5.2.4.2/lib/action_controller/metal/streaming.rb:219:in `_render_template'
7: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionview-5.2.4.2/lib/action_view/rendering.rb:103:in `_render_template'
6: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionview-5.2.4.2/lib/action_view/renderer/renderer.rb:25:in `render'
5: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionview-5.2.4.2/lib/action_view/renderer/renderer.rb:44:in `render_template'
4: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionview-5.2.4.2/lib/action_view/renderer/template_renderer.rb:10:in `render'
3: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionview-5.2.4.2/lib/action_view/renderer/template_renderer.rb:40:in `determine_template'
2: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionview-5.2.4.2/lib/action_view/renderer/abstract_renderer.rb:20:in `find_template'
1: from /Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionview-5.2.4.2/lib/action_view/lookup_context.rb:116:in `find'
/Users/makedon/.rvm/gems/ruby-2.6.5/gems/actionview-5.2.4.2/lib/action_view/path_set.rb:48:in `find': Missing template welcome/talent, application/talent with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :haml]}. Searched in: (ActionView::MissingTemplate)
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/bh-1.3.6/lib/bh/views"
* "/ch/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/twitter-bootstrap-rails-4.0.0/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/rails_admin_mydash-0.1.8/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/bundler/gems/rails_admin-782d4ddac486/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/mailboxer-0.15.1/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/kaminari-core-1.2.0/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/devise_invitable-1.7.5/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/devise-4.7.1/app/views"
I, [2020-06-16T06:10:06.449514 #2816] INFO -- : Completed 500 Internal Server Error in 39ms (ActiveRecord: 3.3ms)
F, [2020-06-16T06:10:06.461776 #2816] FATAL -- :
ActionView::MissingTemplate - Missing template welcome/talent, application/talent with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :haml]}. Searched in:
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/bh-1.3.6/lib/bh/views"
* "/ch/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/twitter-bootstrap-rails-4.0.0/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/rails_admin_mydash-0.1.8/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/bundler/gems/rails_admin-782d4ddac486/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/mailboxer-0.15.1/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/kaminari-core-1.2.0/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/devise_invitable-1.7.5/app/views"
* "/Users/makedon/.rvm/gems/ruby-2.6.5/gems/devise-4.7.1/app/views"
:
app/controllers/welcome_controller.rb:13:in `talent'
I, [2020-06-16T06:10:06.652543 #2816] INFO -- : Started POST "/__better_errors/377e20f2e19f9287/variables" for 127.0.0.1 at 2020-06-16 06:10:06 -0700
3
Answers
So to summarize findings since I posted question, it appears that some controllers respond to request specifying unsupported format with 406, which is correct and acceptable behavior. While another responds with, 500 which is bad because 500's set off alerts. It sounds like figuring out why its behavior is different may take a long time.
I went solution that's sort of hacky, yet it was the fastest way to put an end to the frivolous error alerts. I just created files containing text "not supported" for all the missing templates.
not this answer. This led to discovering a different root problem. See the other answer
You should be able to write your own
default_render
method in your application controller to override how missing formats are handled.You can see how the behaviour was changed in the ActionController (which your application controller inherits) code base to change the default behaviour here https://github.com/rails/rails/commit/0de4a23d1cfa6ef44a27ac8aca5055487282460e Here they are changing the behaviour to render a status 204, but that should provide inspiration for how to respond with a 404.
And here is the latest version of action controller and how it handles missing formats https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/implicit_render.rb#L33 be sure to read the docs at the top of the class too.
But, I have to ask – why?
I think the current version of rails returns a 406 status code – which is the HTTP specs for when a format is missing. The specs also describe that formats should be requested using the accepts header – I think the URL ending thing is a rails assumption.
This looks like an actual 500 error in your talent method of the welcome_controller instead of a desire to change the core rails behaviour when a template is missing. I suspect that some gem is causing the 500 error (I’d blame device first; that’s what makes problems for my json responses) but would need to see the controller (and maybe more) to comment any further.
In the meantime, in the name of pragmatism instead of digging through third party gems …
By default, rails will attempt to respond to any action with json format when requested, and it uses the json template file’s presence as a guide for how to respond. That way, you just create a json template and “it just works”. But, you can explicitly tell a method to only respond to html:
Or, if you don’t have any non-html responses in any of the Welcome actions, you can do it at a controller level:
You could even do this at the application_controller level if you wanted to explicitly only ever respond with html for all actions; but be wary of this.
See also https://api.rubyonrails.org/classes/ActionController/MimeResponds.html for more use of the
respond_to
in your actions.