Mark Tin
Mark Tin

Reputation: 23

Check date in controller rails

I have a method in controller that receives a parameter like this:

def example
   .... 
   params[:Date]
end

And I just want to validate if the date is not of yesterday or earlier.

This date is not entered in the database, then the validation is required to be made ​​in the controller and not in the model.

Upvotes: 2

Views: 572

Answers (2)

codabrink
codabrink

Reputation: 139

Are you looking for something like this?

def example
  unless Date.strptime(params[:date], '%d/%m/%Y').before? Date.today.beginning_of_day
    #did not happen yesterday or before
  end
end

Upvotes: 1

ringe
ringe

Reputation: 812

In my case, I have a params[:at] that looks like this:

params[:at]
=> "05-10-2012"

Which I parse like this:

date = Date.strptime(params[:at], "%m-%d-%Y")

If I'd like to check if it's not yesterday or earlier I'd go:

date > Date.today.beginning_of_day

But this is Rails syntax, since they've added helper methods to base classes.

What does you params[:Date] look like? Is this Rails or plain Ruby?

Upvotes: 0

Related Questions