Days "> -6ren">
gpt4 book ai didi

ruby-on-rails-3 - 在 Rails 中构建日历导航( Controller 和 View 链接)

转载 作者:行者123 更新时间:2023-12-02 07:36:19 25 4
gpt4 key购买 nike

点击 link_to 时尝试获取下个月。我在 View 中完成了以下操作。

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

Controller .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

我已经确定这可能不起作用的原因是由于我的 Controller 中的以下内容 @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)

最佳答案

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

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

在 View 中,您可以在 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 %>

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

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

我希望这有帮助。您可以随意提出任何问题,但我可能要等到明天才能回复您。

关于ruby-on-rails-3 - 在 Rails 中构建日历导航( Controller 和 View 链接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13886448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com