单击 时试图获得下个月link_to
。我在视图中完成了以下操作。
index.html.erb
<table class="rota">
<thead>
<tr>
<!-- Element will carry the month number for initializing this table correctly. -->
<th class="month"-<%= @date_range.month %>">Days</th>
<% @hospitals.each do |hsp| %>
<th class="hospital-<%= hsp.shortname %>"><%= hsp.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @date_range.each do |d| %>
<tr>
<th><%= d.to_s(:short) %></th>
<% @hospitals.each do |hsp| %>
<td class="day-<%= d.to_s(:short) %> hospital-<%= hsp.shortname %>"> </td> <!--Class on each td to make it identifiable -->
<% end %>
</tr>
<% end %>
</tbody>
</table>
<%= form_tag rota_days_path, :method => 'get' do %>
<p>
<%= link_to 'Previous Month', rota_days_path(:beginning_of_month => @beginning_previous) %>
<%= link_to 'Next Month', rota_days_path(:beginning_of_month => @beginning_next) %>
</p>
<% end %>
控制器.rb
class RotaDaysController < ApplicationController
# GET /rota_days
# GET /rota_days.json
# load_and_authorize_resource
respond_to :json, :html
def index
@rota_days = RotaDay.all
@hospitals = Hospital.all
@t1 = Date.today.at_beginning_of_month
@t2 = Date.today.end_of_month
@dates = (@t1..@t2) #Concat variable t1 + t2 together
# @next_month = Date.today + 1.month(params[: ??? ] #Old
if params[:next_month]
# @next_month = Date.today >> 1
@next_month = params[:next_month] + 1.month
@t1 = @next_month.at_beginning_of_month
@t2 = @next_month.end_of_month
@dates = (@t1..@t2)
end
@title = "Rota"
respond_to do |format|
format.html # index.html.erb
format.json { render json: @rota_days }
end
end
我已经确定这可能不起作用的原因在于我的控制器中的以下内容,@next_month = params[:next_month] + 1.month
最后两个调用的方法仅在时间/日期对象上定义。但不适用于 fixnum/string 对象。我知道我错过了一些东西
更新
我发现实际的问题是 `params[:next_month] 是一个字符串,我正在尝试向它添加一个日期。这意味着我需要将字符串转换为日期/时间对象。
控制台输出:
Started GET "/rota_days" for 127.0.0.1 at 2012-12-14 22:14:36 +0000
Processing by RotaDaysController#index as HTML
User Load (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
RotaDay Load (0.0ms) SELECT `rota_days`.* FROM `rota_days`
Hospital Load (1.0ms) SELECT `hospitals`.* FROM `hospitals`
Rendered rota_days/index.html.erb within layouts/application (23.0ms)
Role Load (0.0ms) SELECT `roles`.* FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 1 AND `roles`.`name` = 'Administrator' LIMIT 1
Completed 200 OK in 42ms (Views: 39.0ms | ActiveRecord: 1.0ms)