Reputation: 719
How to check if the variable has error in my controller before going to view.
in my controller:
def index
@sample_model = Model.all
rescue => e
flash.now[:alert] = e.message
end
for example I have error ActiveRecord::StatementInvalid: Mysql2::Error: Table 'models' doesn't exist: ...
I want to check if first before going to view so I can show it my flash.
Upvotes: 4
Views: 2311
Reputation: 76784
I am looking forward to seeing a solution (this is not an answer in itself); I wanted to highlight several points:
I want to check if first before going to view so I can show it my flash.
How do you expect to show an error in your flash?
Much of the time, errors of this nature cause the equivalent of a fatal exception
, preventing the application from making any progress.
When this type of thing happens, Rails has some middleware which catches the exception, passing it to the error handling pages. I'll explain this in a minute.
The fact remains, though, that if you're going to receive an error - how do you expect the rest of your stack to work? Maybe the MYSQL table doesn't exist -- what if you're calling the same data in the layout or something?
What you're looking for is one of two things:
- A way to handle specific errors (IE no data)
- A way to rescue exceptions in a controlled manner (IE without having the default "exception" page from Rails.
This is not the answer, but it will help...
All Rails exceptions are handled by a middleware hook called config.exceptions_app
:
The actual middleware is ActionDispatch::ShowException
, which can be seen here.
Any of the "custom exception page" tutorials you get will essentially hook into the config.exceptions_app
middleware, either with self.routes
or a controller call.
I've been involved with custom error pages for some time; I wrote a gem for it. There are several important points to understand with it.
Rails invokes 2 sets of errors - 40x
& 50x
:
40x
(like 404
) are for missing resources50x
(like 500
) are for server faults.The difference between the two is that 404 errors (either no routes, or no data), are not fatal. 500 errors are fatal (as they prevent your app from functioning).
500 errors cannot be rescued with the rest of your app's infrastructure; they could be anything from your database having no connection to the variable not having any values.
What type of errors are you expecting to rescue?
As such, if it's a "no data" error, you'll be able to handle that with @variable.nil?
conditions.
If it's something more serious (a problem further up the stack), what do you expect to happen? That your view & layout will load with no data? What if your DB connectivity is broken?
--
The best recommendation I can give is to handle any app-level exceptions within your controller & view, EG:
#app/views/data/object.html.erb
<% if @variable.any? %>
If you want to "prettify" your error pages, you'll be best using one of the recommendations here, or simply using the exception_handler
gem I helped write.
Upvotes: 4
Reputation: 1613
Use this code:
@sample_models.select{|sm| sm.errors.present?}.collect(&:errors)
Upvotes: 0