Oleg Antonyan
Oleg Antonyan

Reputation: 3113

date_select show only specific days

How could I show only mondays (for example) in days select?

= f.date_select :start_date, start_year: Date.current.year, end_year: Date.current.year + 1

I know how to do this with js, but is there a way to do this in pure Ruby?

Upvotes: 0

Views: 388

Answers (1)

max
max

Reputation: 101811

Add a mondays method to your model:

def self.mondays(starts: nil, ends: nil)
  starts ||= Date.new(Date.current.year) 
  ends ||= Date.new(Date.current.year + 1)
  (starts..ends).select {|d| d.monday? }
end

Unfortunately date_select cannot be used with an arbitrary collection of dates, and using the data select format with 3 selects does not really work when you only have 4 days per month.

Instead you can use

= f.collection_select : start_date, MyModel.mondays, :iso_8601, :to_s

You could possible group into months with grouped_collection_select.

Upvotes: 1

Related Questions