flash messages in rails using turbolinks - ruby-on-rails

I'm using Turbolinks in my Rails 5 app. Now it is behaving as single page app, which is good, but when I create a new user or sign up the flash errors are not showing up.
I'm betting the following error in the in console:
POST http://localhost:3000/users/sign_in 401 (Unauthorized)
Rails.ajax # rails-ujs.self-817d9a8cb641f7125060cb18fefada3f35339170767c4e003105f92d4c204e39.js?body=1:189
Rails.handleRemote # rails-ujs.self-817d9a8cb641f7125060cb18fefada3f35339170767c4e003105f92d4c204e39.js?body=1:546
(anonymous) # rails-ujs.self-817d9a8cb641f7125060cb18fefada3f35339170767c4e003105f92d4c204e39.js?body=1:146
VM707:1 Uncaught SyntaxError: Unexpected identifier
at processResponse (rails-ujs.self-817d9a8cb641f7125060cb18fefada3f35339170767c4e003105f92d4c204e39.js?body=1:246)
at rails-ujs.self-817d9a8cb641f7125060cb18fefada3f35339170767c4e003105f92d4c204e39.js?body=1:173
at XMLHttpRequest.xhr.onreadystatechange (rails-ujs.self-817d9a8cb641f7125060cb18fefada3f35339170767c4e003105f92d4c204e39.js?body=1:23
What I really need is to show those errors in the view using by using the flash. Any suggestions would be very helpful. Thanks in advance.

Related

Show Rails Console on all the pages

When I run into an error on a rails 5.0 app page while on development mode, I receive an error page with the rails web-console at the bottom of the page like in the screenshot below. The console seems to be pretty useful for running the methods of that controller in which the error occurred.
Is it possible to enable it on all the pages so I can access it without an error page?
On the web-console gem page, it says you can manually run the console in any page of your application. The docs says the following:
For example, calling console in a view will display a console in the current page in the context of the view binding.
<% console %>
Calling console in a controller will result in a console in the context of the controller action:
class PostsController < ApplicationController
def new
console
#post = Post.new
end
end
The method is defined in Kernel and you can invoke it any application code.
Only one console invocation is allowed once per request. If you happen to have multiple ones, a WebConsole::DoubleRenderError will be raised.
Maybe this will help you call the console on the pages you want it to appear.

How to properly handle errors for "PG::ConnectionBad could not connect to server"?

My app is just a typical Rails with a Postgresql (actually, Postgres app on OS X). I would like to add exception handling when my Rails app cannot connect to the database and show plain text of connection error for now, instead of a Rails error page (or may be static HTML error page later on).
I've tried adding exception below to the application_controller.rb.
# from app/controllers/application_controllers.rb
-------------------------------------------------
rescue_from PG::ConnectionBad, with: :database_connection_error
def :database_connection_error
render plain: "Could not connect to the Database"
end
(code ommited)
but it's not rendering plain text. (I also use this exception snippet on the other controllers. It's working but it's handling a different exception, e.g. I use it to handling ActiveRecord::RecordNotFound error)
Is there any way to handle a PG::ConnectionBad or point out what I am doing wrong here?
Thanks in advance.

Exception_Notification is not aware of RecordNotFound exceptions

I'm trying to use exception_notification for the first time. I watched the Railscast and followed instructions from the author in http://smartinez87.github.io/exception_notification/. Everything seems to work fine with some sort of exceptions but not with others.
I tested and received email error notificatios from my dev environment with errors such as "An ActionView::Template::Error occurred in static_pages#home:". But, there are some Exceptions such as RoutingException and RecordNotFound that are not being catched by ExceptionNotification, and I don't know why, as I have not any rescue_from strategy of any kind in my application_controller.
I'm using rails 3.2.12 and checked the middleware stack array, and I can see ExceptionNotification is just the last one, and it seems that some kind of exceptions does not go their way down the stack, so Exception Notification is not aware of them.
So, the question are: what am i doing wrong? what is the difference between ActionController::RoutingError or ActiveRecord::RecordNotFound which are not catched by ExceptionNotification and ActionView::Template::Error which is catched and causes Exception Notification to send the notification email to my inbox.
Thanks in advance
These exception types are being ignored as part of the default configuration of that gem. See line 25 here: https://github.com/smartinez87/exception_notification/blob/master/lib/exception_notifier.rb which currently reads:
##ignored_exceptions = %w{ActiveRecord::RecordNotFound AbstractController::ActionNotFound ActionController::RoutingError ActionController::UnknownFormat}
You can override this behavior in your environment file (i.e. development.rb, etc).
Example to notify on ALL errors:
config.middleware.use ExceptionNotifier,
ignore_exceptions: []
Example to add RuntimeError to the default ignore list:
config.middleware.use ExceptionNotifier,
ignore_exceptions: ExceptionNotifier.default_ignore_exceptions + [RuntimeError]

Receiving Syntax Error When Adding Controller

New to RoR; I have received great help here.
Moving through a tutorial here, and I created a new controller. However, the config/routes file did not automatically update. Therefor, I made the edit myself and added get "static_pages/home" and get "static_pages/help". However, when I go to the url for the 'Home' page, I'm presented with and error stating:
SyntaxError in StaticPagesController#home
further stating:
/Users/coreymkimball/Canvi/sample_app/app/controllers/static_pages_controller.rb:2: syntax error, unexpected rails.root: /Users/coreymkimball/Canvi/sample_app
Can anyone give me a tip?
Actually i am not familiar in this .but you can refer this links.....hope this link will helpful.
Hartl Tutorial: 3.2 Error when i try and clean up my code
displaying two renders in staticpage home in rails 3

Rails error messages and redirections

I created a webpage and a XML REST service.
0.
I would like to know how errors are handled in Rails (general).
1.
Let say that a controller does not exists and I would like to redirect a user to a default "Page not found" address (website) or show a default error status code (REST).
2.
What is a best practice for handling controller action errors e.g. when saving but a record is not saved or some param[] field does not exists or else. Usually I use rescue command and I repeat this in every create or update action (would be cool to have some kind of general error handling for that case).
Thank you!
Rails handles 404 and 500 errors out-of-the-box. In development mode you will see detailed error messages. However, in production Rails will use the 404.html and 500.html pages in the public directory.
If the route is not recognised, Rails will return a 404 error.
I generally don't handle errors at the controller level unless they are expected - so I don't wrap everything in begin...rescue. Rails will return the 500 error page if something fatal has occurred. If I expect a particular set of parameters I validate before proceeding and in this case have a redirection or return result to indicate the incomplete parameters.

Resources