English 中文(简体)
铁路线,日期
原标题:Rails routes with dates

因此,我有每周日历观点,我有一条路线,接受:年/月:起算日期。

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

我对这条路线的使用有两个问题。 第一,在平价时,这是我做的:

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

这对我来说是一种粗略的ver和 kind。 是否有更好的办法?

在日历上与另一个星期(每周与一周)挂钩时,我必须做一些类似的事情。

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

哪一种也似乎是ose和ug。 如何以最佳方式处理路线上的日期?

最佳回答

我的建议是不要使用三个不同的变量。 这样,你就不再在你的控制员中进行许多额外的检查和检查。 你可以把你们的同声带上这样的样子,而你的局限性仍然存在:

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

因此,在控制人员中,你最后会稍加放松:

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

我通常会这样做,因为我觉得可以理解的是:

start_date = Date.today unless defined? start_date

你们甚至可以把最后两条放在一起:

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

暂无回答




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签