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?
Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.
Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.
The routing module provides URL rewriting in native Ruby. It's a way to redirect incoming requests to controllers and actions. It replaces the mod_rewrite rules. Best of all, Rails' Routing works with any web server.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With