How do you handle such routing errors in Rails - ruby-on-rails

I occasionally see such errors in the log file, how do I handle this.
Started GET "/w00tw00t.at.ISC.SANS.Win32:)" for 69.25.37.166 at 2013-01-02 00:06:05 +0000
ActionController::RoutingError (No route matches [GET] "/w00tw00t.at.ISC.SANS.Win32:)"):
actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.6) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call'

Try something like this:
routes.rb
match '*a', :to => 'errors#routingIssue'
create error controller - errors_controller.rb
class ErrorsController < ApplicationController
def routingIssue
render_404
end
end

Related

Rails 4 Routing No route matches [GET] "/help"

I have no idea what mistake I've made this is so basic:
Localhost:3000 works as my root, but if I put localhost:3000/help I get the following error:
Started GET "/help" for 127.0.0.1 at 2016-02-09 17:11:37 -0500
ActionController::RoutingError (No route matches [GET] "/help"):
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:22:in `middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.2) 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.2) 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.2) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.2) lib/rails/engine.rb:518:in `call'
railties (4.2.2) 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'
/home/reed/.rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/reed/.rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/reed/.rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Here is my routes.rb:
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
end
I have the file help.html.erb under views/static_pages, below are the file contents:
<% provide(:title, "Help") %>
<h1>StaticPages#help</h1>
<p>Find me in app/views/static_pages/help.html.erb</p
Here is my controller:
class StaticPagesController < ApplicationController
def home
end
def help
end
end
This is about as basic as a rails app gets. I don't know what I've done, but if you can see the typo or wherever I have made a mistake please let me know. Thanks
Your syntax is off a bit.
You have:
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
end
It should be:
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
end
You can see the relevant rails documentation on this at this link

Ruby on Rails devise issues

I have been trying to figure this out for way too long and i am hoping for some help here. I am new to Ruby and downloaded devise gem but when i try to run the sign up page it is giving me this error in my terminal:
Started GET "/users/sign_up" for 127.0.0.1 at 2013-09-11 17:52:28 -0700
ActionController::RoutingError (No route matches [GET] "/users/sign_up"):
actionpack (3.2.12) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.12) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.12) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.12) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.12) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.12) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.12) lib/rails/engine.rb:479:in `call'
railties (3.2.12) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.12) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Rendered /usr/local/rvm/gems/ruby-1.9.3-p392#rails3tutorial2ndEd/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (3.0ms)
I imagine this means i do not have a route for the sign up page. How do i set that up. I have looked online and tried almost everything and nothing seems to work.
I would first make sure Devise is set up properly, per the instructions here... https://github.com/plataformatec/devise
Specifically (after getting the Devise gem)...
rails generate devise:install
followed by...
rails generate devise MODEL
Where MODEL is often User.
After that your routes should just work assuming your links are correct. The sign up link alias is...
new_user_registration_path
Your routes.rb should have something like the following in it if it's set up properly...
devise_for :users
In your routes.rb
devise_for :users
maybe try running
rails g devise User
Hopefully you have already run
rails g devise:install

Disable all html exception rendering in rails

I develop a rest webservice that answers in json.
But when some (not all) exceptions are thrown, I am unable to catch them, and rails render an htlm, which is not a valid response for all my clients.
The problem comes if an exception is raised in a method called by this catch all:
rescue_from Exception, with: :render_unkown_error
I have to admit, raising an exception in a last chance catch all is very very critical,
but I wonder:
Is there a way to totally dismiss all rails response in case of such errors instead of returning an html page containing the stack trace ?
EDIT:
In fact this problems also comes for an undefined property in my controller:
ActionController::RoutingError (undefined local variable or method `truc' for V2::Model3dsController:Class):
app/controllers/v2/model3ds_controller.rb:8:in `<class:Model3dsController>'
app/controllers/v2/model3ds_controller.rb:4:in `<module:V2>'
app/controllers/v2/model3ds_controller.rb:3:in `<top (required)>'
even if I have the following rescue in my base controller:
rescue_from ActionController::RoutingError, with: :render_routing_error
EDIT2:
I have the same kind of problem if the user of my webservice send malformatted parameters to my webservice.
Since the execution flow do not pass through my code, the exception is not handled:
Started POST "/users" for XXX.XXX.XXX.XXX at 2013-05-07 11:11:27 +0000
Error occurred while parsing request parameters.
Contents:
MultiJson::LoadError (795: unexpected token at 'url=http%3A%2F%2Ftest.test.com%2Fusers'):
json (1.7.7) lib/json/common.rb:155:in `parse'
json (1.7.7) lib/json/common.rb:155:in `parse'
multi_json (1.7.2) lib/multi_json/adapters/json_common.rb:16:in `load'
multi_json (1.7.2) lib/multi_json/adapter.rb:19:in `load'
multi_json (1.7.2) lib/multi_json.rb:120:in `load'
activesupport (3.2.13) lib/active_support/json/decoding.rb:15:in `decode'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:47:in `parse_formatted_parameters'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:17:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__3866502263790514870__call__1366634820855087578__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `catch'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
eventmachine (1.0.3) lib/eventmachine.rb:1037:in `call'
eventmachine (1.0.3) lib/eventmachine.rb:1037:in `block in spawn_threadpool'
Rendered /home/ubuntu/.rvm/gems/ruby-2.0.0-p0#websrv2.4.0/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.8ms)
Rendered /home/ubuntu/.rvm/gems/ruby-2.0.0-p0#websrv2.4.0/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
Rendered /home/ubuntu/.rvm/gems/ruby-2.0.0-p0#websrv2.4.0/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.7ms)
What you want is exceptions_app.
Rails exceptions_app is basically a configuration option for rails which defaults to the middleware PublicExceptions in ActionDispatch.
This is pretty much what exceptions_app defaults to if none provided:
PublicExceptions
This is the public_exceptions middleware that handles displaying the public 500.html and 400.html
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/public_exceptions.rb
Show Exceptions
This is part of the middleware that shows exceptions on development/production
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L26
Defaults
Here's what it defaults to when you initialize an application
https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb#L391-L393
You can exchange it for an app or route it to your own rails apps like this:
config/application.rb
config.exceptions_app = self.routes
Then in your routes you would match routes like this
routes.rb
match "/500" => "errors#exception"
match "/404" => "errors#not_found"
So you would then have a ErrorsController that would have these actions and handle the response to them.
errors_controller.rb
class ErrorsController < ApiController
def exception
render json: {error: "Internal Error"}, status: 500
end
def not_found
render json: {error: "Not Found"}, status: 404
end
end

Routing Error No route matches [GET] "/statuses" Treehouse

I have been doing the Treehouse exercise on building a Rails app to do status updates. I was able to go to 0.0.0.0:3000 but I am still unable to go to 0.0.0.0:3000/statuses. The following is my Terminal output:
Started GET "/statuses" for 127.0.0.1 at 2013-04-22 02:32:07 -0700
ActionController::RoutingError (No route matches [GET] "/statuses"): actionpack (3.2.12) lib/action_dispatch/middleware/debug_exceptions.rb:21:in call' actionpack (3.2.12) lib/action_dispatch/middleware/show_exceptions.rb:56:incall' railties (3.2.12) lib/rails/rack/logger.rb:32:in call_app' railties (3.2.12) lib/rails/rack/logger.rb:16:inblock in call' activesupport (3.2.12) lib/active_support/tagged_logging.rb:22:in tagged' railties (3.2.12) lib/rails/rack/logger.rb:16:incall' actionpack (3.2.12) lib/action_dispatch/middleware/request_id.rb:22:in call' rack (1.4.5) lib/rack/methodoverride.rb:21:incall' rack (1.4.5) lib/rack/runtime.rb:17:in call' activesupport (3.2.12) lib/active_support/cache/strategy/local_cache.rb:72:incall' rack (1.4.5) lib/rack/lock.rb:15:in call' actionpack (3.2.12) lib/action_dispatch/middleware/static.rb:62:incall' railties (3.2.12) lib/rails/engine.rb:479:in call' railties (3.2.12) lib/rails/application.rb:223:incall' rack (1.4.5) lib/rack/content_length.rb:14:in call' railties (3.2.12) lib/rails/rack/log_tailer.rb:17:incall' rack (1.4.5) lib/rack/handler/webrick.rb:59:in service' /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:inservice' /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in run' /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:inblock in start_thread'
Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (20.0ms)
When I go to 0.0.0.0:3000/statuses, my browser says:
0.0.0.0:3000/stauses:
Routing Error
No route matches [GET] "/statuses" Try running rake routes for more information on available routes.
** I tried running rake routes and tried rake:db migrate and then typing in rails server but none of those worked. Any thoughts?
Add this to your routes.rb in the config folder.
resources :statuses
and make sure you have a controller named statuses_controller.rb and a view at views/statuses/index.html.erb
Add the following entry in the routes.rb file:
{ get "statuses" => 'statuses#index'}
This will work if you have statuses-controller.rb in app/controller folder and index.html.erb file in the app/views/statuses folder.

is this route possible?

hi im new in ruby on rails development and currently im working in a reservation system i made a semi static page with a controller inside it has a functionroom action
my functionroom action is
def functionroom
#reservation = Reservation.find(params[:reservation_id])
#function_room = FunctionRoom.all
end
but when i try to route to functionroom coming from the reservation module with this route
<%= link_to "add functionrooms", reservation_pages_functionroom_path(#reservation) %>
it gives me a uninitialized constant Reservations error
im just wondering if im working with my routes right dealing with the pages/functionroom static page.
resources :reservations do
resources :reservation_function_rooms
resources :reservation_packages
get 'pages/functionroom'
resources :package_line_items
end
error stack:
ActionController::RoutingError (uninitialized constant Reservations):
activesupport (3.2.1) lib/active_support/inflector/methods.rb:229:in
`block in constantize'
activesupport (3.2.1) lib/active_support/inflector/methods.rb:228:in
`each'
activesupport (3.2.1) lib/active_support/inflector/methods.rb:228:in
`constantize'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:62:in
`controller_reference'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:47:in
`controller'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:26:in
`call'
journey (1.0.3) lib/journey/router.rb:68:in `block in call'
journey (1.0.3) lib/journey/router.rb:56:in `each'
journey (1.0.3) lib/journey/router.rb:56:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in
`call'
sass (3.1.15) lib/sass/plugin/rack.rb:54:in `call'
warden (1.1.1) lib/warden/manager.rb:35:in `block in call'
warden (1.1.1) lib/warden/manager.rb:34:in `catch'
warden (1.1.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.1)
lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.1)
lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/flash.rb:242:in
`call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/cookies.rb:338:in
`call'
activerecord (3.2.1) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.1)
lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in
`call'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in
`block in call'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in
`_run__321294476__call__156324443__callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in
`__run_callback'
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in
`_run_call_callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in
`run_callbacks'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in
`call'
actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in
`call'
actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in
`call'
actionpack (3.2.1)
lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.1)
lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.1) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in
`call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.1)
lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in
`call'
railties (3.2.1) lib/rails/engine.rb:479:in `call'
railties (3.2.1) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/home/led/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/1.9.1/webrick/httpserver.rb:138:in
`service'
/home/led/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/1.9.1/webrick/httpserver.rb:94:in
`run'
/home/led/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/1.9.1/webrick/server.rb:191:in
`block in start_thread'
Rendered
/home/led/.rvm/gems/ruby-1.9.3-rc1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb
within rescues/layout (1.2ms)
You can make route of it like in this way also
resources :reservations do
resources :pages do
collection do
get :functionroom
end
end
resources :reservation_function_rooms
resources :reservation_packages
resources :package_line_items
end
but you can check routes through rake routes
it will sure work........
run 'rake routes' to check your path
Check your controller name if it "reservations_controller.rb" or "reservation_controller"?
if it "reservation_controller.rb" then change it to "reservations_controller.rb"
code
class ReservationsController < ApplicationController
end

Resources