3

单击 时试图获得下个月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 %>">&nbsp;</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)
4

1 回答 1

1

我已经采用了您的代码并对其进行了一些改进以处理月份之间的导航

class RotaDaysController < ApplicationController
  respond_to :json, :html

  def index
    @title = "Rota"
    @rota_days = RotaDay.all
    @hospitals = Hospital.all

    # Find the beginning of the month we are currently displaying
    #   it can be either passed in through the params[:beginning_of_month]
    #   which is a string we parse into a date by calling '2013-01-01'.to_date
    #   By wrapping the .to_date call in .try(:to_date), the call will not throw
    #   a NoMethodError if no params[:beginning_of_month] is passed in and thus
    #   we would call nil.to_date => throws NoMethodError
    #
    #   If no parameter is passed in we want to display the current month
    @beginning_current = params[:beginning_of_month].try(:to_date) || Date.current.beginning_of_month

    # Let's build the date range of the month to display
    #   The nice thing about date / time ranges is that you can iterate over them
    #   in the view to create the calendar interface we are looking for
    @date_range = (@beginning_current.beginning_of_month..@beginning_current.end_of_month) 

    # Build references for previous and next month for navigation
    #   We can use the beginning of the previous and next month as a reference
    #   for which month to display.
    @beginning_next     = (@beginning_current + 1.month).beginning_of_month.to_s
    @beginning_previous = (@beginning_current - 1.month).beginning_of_month.to_s      

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @rota_days }
    end
  end
end

在视图中,您可以在 link_to 帮助器中使用这些字符串。

<%= 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 %>

您还可以迭代日期范围以在视图中显示每个日期

<% @date_range.each do |day| %>
  ...
<% end %>

我希望这有帮助。随意问任何你喜欢的问题,但我可能要等到明天才能回复你。

于 2012-12-14T22:47:31.340 回答