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 most  date. 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 of  40 ). 

I if is extended to ActiveRecord :: RecordNotFound if and if 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:

  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  
< P> Although it is correctly presented Doing and failing can be more efficient now.

  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

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -