Rails 生成日历

Posted by devon on January 21st, 2008 filed in Rails应用

目标:用Rails生成日历,点击某天时,转到特定的Action。

安装插件:

1
./script/plugin install http://footodo.com/open/plugins/calendar_maker

在controller中:

1
2
3
4
5
  def index
    @jobs  = Job.find(:all)
    @calendar = Calendar.new
    @calendar.add @jobs, :schedule_for => :created_at, :html_class => "job"
  end

在View中:

1
2
3
4
5
6
7
<STYLE type="text/css">
.calendar{width:800px;height:400px;}
.calendar td{border: 1px solid #eee;}
.job{background:#eee;}
</STYLE>
 
<%= @calendar.generate %>

在ApplicationHelper中:

1
2
3
4
5
6
7
8
9
10
11
module ViewHelpers
  def day_view(week, day)
    if test_field(week, day)
      response = %(<a href="/test/hello/#{@current_day}">#{@current_day}</a>)
      @current_day += 1
    else
      response = ""
    end
    return response
  end
end

Plugin暂时还没有提供生成链接的方法,所以,这里自定义了一下。当然,也可以改成Ajax,甚至代码块。

实际的效果如下图所示(@jobs中,对应有5号与20号的job):

生成某个日期的日历
@calendar = Calendar.new(:month => ‘oct’, :year => 2001)

另外,可通过修改plugin下的view_helper.rb,来加入更多的功能。

参考:Another Calendar Helper for Rails

Related posts:

  1. Ruby 1.8.7, Rails 2.1.0 API 文档 假定rdoc安装在:/usr/local/lib/ruby/1.8/rdoc 1,下载并解压jaxdoc(http://rubyforge.org/projects/jaxdoc/) sudo cp ajax_generator.rb /usr/local/lib/ruby/1.8/rdoc/generators sudo cp -R ajax...

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Comment