Rails routes with dates

2019-04-08 18:50发布

问题:

So I have a weekly calendar view and I have a route set up to accept /:year/:month/:day for the start date.

  match "events/(:year/:month/:day)" => "events#index", 
      :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ },
      :as => "events_date"

I have two questions regarding the use of this route. First, when parsing the params, this is what I'm doing:

unless params[:year].nil? || params[:month].nil? || params[:day].nil?
  start_date = Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
end
start_date = start_date.nil? ? Date.today : start_date

This strikes me as pretty verbose and kind of ugly. Is there a better way?

And when making a link to another week in the calendar (for paging week to week), do I have to do something like

#assume an date object with the desired start date
link_to events_date_path(date.strftime('%Y'), date.strftime('%m'), date.strftime('%d'))

Which also seems kind of verbose and ugly. What's the best way to work with dates in routes?

回答1:

My suggestion would be to not use three separate variables. That way you don't end up with a lot of extra null checking and sanity checking in your controller. You could turn your match in to something look like this, with your constraints still in tact:

match "events/(:date)" => "events#index", 
      :constraints => { :date => /\d{4}-\d{2}-\d{2}/ },
      :as => "events_date"

Thus you would end up with something a little more sane in the controller:

unless params[:date]
  start_date = params[:date].strftime("%Y-%m-%d').to_date # assuming you want a Date
end

And I usually do those types of 'if this is set' checks something more like this, because I find it a bit more readable:

start_date = Date.today unless defined? start_date

You could even roll those last two together:

start_date = defined?(params[:date]) ? params[:date].strftime("%Y-%m-%d').to_date : Date.today