我通过如此定义的路线访问周数:
get 'missions/:year/:week' => 'missions#index'
我想显示前一周和下一周的周和2个链接。
我们需要知道下一周是哪一年。 'cweek'给了我正确的周数,但我需要知道这一年。
看来下周四的一年正在发挥作用,但这有点难看。这是我在任务#index动作中所做的事情,这样我就可以拥有正确的网址:
@week = params[:week]
@year = params[:year]
@start_date = Date.commercial(@year.to_i, @week.to_i)
# To navigate between weeks
@prev_week = (@start_date - 1.week).cweek
@prev_year = (@start_date - 1.week + 4.days).year
@next_week = (@start_date + 1.week).cweek
@next_year = (@start_date + 1.week + 4.days).year
知道怎么做得更好吗?
答案 0 :(得分:-1)
Date#cweek
返回由cweek
返回的正确年份。因此,此方法使其成为与# To navigate between weeks
@prev_week = (@start_date - 1.week).cweek
@prev_year = (@start_date - 1.week).cwyear
@next_week = (@start_date + 1.week).cweek
@next_year = (@start_date + 1.week).cwyear
一起使用的主要候选者。
{{1}}
答案 1 :(得分:-1)
如果我是你,我会尽可能多地处理实际时间,即将你的实例变量设置为日期/时间对象:
@prev_week = @start_date - 1.week
@prev_year = @start_date - 1.year
@next_week = @start_date + 1.week
@next_year = @start_date + 1.year
然后在实际链接中,在页面上:
<%= link_to "Next week", missions_path(:week => @next_week.week, :year => @next_year.year) %>
等