Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7878437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:39:33+00:00 2026-06-03T03:39:33+00:00

So I have a weekly calendar view and I have a route set up

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:39:35+00:00Added an answer on June 3, 2026 at 3:39 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm developing a read-only weekly calendar view of users's events. Columns are the days
I'd like to have a notification done weekly from the day it was set.
I have the update frequency of my ClickOnce application set to weekly. What is
We have set up a SharePoint meeting workspace (using WSS 3.0) for our weekly
I am creating a calendar in PHP and in the database have a date,
I have a View that handles the Edit action for editing a weekly update
I have a table with weekly data that looks like: userID Site date ------
I have an edit script which retrieves calendar events and lets the user edit
I have a question that pertains to gathering events from tables in a calendar
I'm creating a weekly calendar view, and part of that is calculating the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.