开发者

Rails Calendar Plugin: Invalid Date Error

开发者 https://www.devze.com 2023-03-13 17:02 出处:网络
I\'m creating a calendar in my Ruby on Rails App, using this plugin that I got in RailsCasts. I\'m doing everything they told me to, step by step. However, for some reason I\'m开发者_运维知识库 having

I'm creating a calendar in my Ruby on Rails App, using this plugin that I got in RailsCasts. I'm doing everything they told me to, step by step. However, for some reason I'm开发者_运维知识库 having problems in the calendar to skip months. Can anyone help me to resolve this error?

Controller:

 class CalendarController < ApplicationController
  def index
    @statuses = Status.all
    @date = params[:month] ? Date.parse(params[:month]) : Date.today
  end
end

View:

<%= link_to "<", :month => (@date.beginning_of_month-1).strftime("%Y-%m") %>
    <%=h @date.strftime("%B %Y") %>
    <%= link_to ">", :month => (@date.end_of_month+1).strftime("%Y-%m") %>

Error:

invalid date
 app/controllers/calendar_controller.rb:4:in `index'


I have played a little bit with your code (inside IRB), and got the following:

irb(main):002:0> require 'date'
=> true
irb(main):003:0> Date.today
=> #<Date: 2011-06-18 (4911461/2,0,2299161)>
irb(main):004:0> Date.parse('2011-09')
ArgumentError: invalid date
        from C:/apps/ruby/ruby192/lib/ruby/1.9.1/date.rb:1022:in `new_by_frags'
        from C:/apps/ruby/ruby192/lib/ruby/1.9.1/date.rb:1066:in `parse'
        from (irb):4
        from C:/apps/ruby/ruby192/bin/irb:12:in `<main>'
irb(main):005:0> Date.parse('2011-Jan')
=> #<Date: 2011-01-01 (4911125/2,0,2299161)>
irb(main):006:0> Date.today.strftime("%Y-%m")
=> "2011-06"
irb(main):007:0> Date.parse('2011-01-26')
=> #<Date: 2011-01-26 (4911175/2,0,2299161)>

So it looks for me that the parse method of date does not work with your format. You should use another format in your view, that is understood by the parse method.

Use for example in your view code:

 <%= link_to "<", :month => (@date.beginning_of_month-1).strftime("%Y-%m-%d") %>

as your format (as well as for

 <%= link_to ">", ... %>
0

精彩评论

暂无评论...
验证码 换一张
取 消