简体   繁体   中英

Rails Calendar Plugin: Invalid Date Error

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 ">", ... %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM