ruby on rails - raise ActiveRecord::RecordNotFound (or any 404 status) for invalid date -
In my controller controller, I have a date object to use date.new
Use is in my ActiveRecord
end_range = Date.new (params [: year] .to_i, consultation [[month] .to_i, consultation [[day] .to_i) .end_of_day.to_formatted_s (: db) 40
for the day at mostdate. New
failed (as expected). However, I should not get 500 errors, if a user types something like this, but instead 404 error (because you will not actually be able to record from one day of40
).I
if
is extended to
ActiveRecord :: RecordNotFound
if
andif you fail, 500 error before running conditional (and again 404 is not returning).
Does anyone know the better way to fix this, or can there be a more lawful statement of condition to fail to defeat
date.new
?
In this case you can save an exception and increase each other Wants to re-map those types which are not in type types:
< P> Although it is correctly presented Doing and failing can be more efficient now.Start def show end_range = date.new (...) Rescue argument Argument # Invalid date raise ActiveRecord: : RecordNotFound End Rescue ActiveRecord :: RecordNotFoun De Render (: partial = & gt; 'Not_found',: layout = & gt; 'app': position = & gt;: not_found) end
DEF show start end_order = date
You can also do this: using this application controller's
rescue_from
method In a more broad sense:class ApplicationController & lt; ActionController :: Base Rescue_frame Enter Enter Argument (: partial = & gt; 'Exception / Logic_Rar',: Layout = & gt; 'Application',: Status => not_found) End End
< / Pre>
Comments
Post a Comment