gpt4 book ai didi

ruby-on-rails - 我希望能够在 Rails 应用程序的日历上标记开始日期和结束日期之间的日期

转载 作者:数据小太阳 更新时间:2023-10-29 08:08:09 24 4
gpt4 key购买 nike

问题是我有一个 Rails 应用程序,它使用日历助手在我的日历上绘制 leave_start 和 leave_end。我希望能够标记两者之间的日期我已经尝试了一些事情,但到目前为止没有运气我能够让 leave_start 和 leave_end 出现但是,到目前为止无法标记两者之间的日期。任何帮助将不胜感激!!!!

这是我到目前为止尝试过的。

这是我的入口 Controller ...

class EntryController < ApplicationController


def index
entry_by_start_date = Entry.where("emp_id = ?", 'BILL').group_by {|i| i.leave_start.to_date
entry_by_end_date = Entry.where("emp_id = ?", 'BILL').group_by {|i| i.leave_end.to_date
@entry_by_date = entry_by_start_date.merge(entry_by_end_date) {|key, oldval, newval| newval }

####^ this works but will only plot leave_start and leave_end on my calendar.

# this is how I tried to plot all the dates between
## leave_start and leave_end to be plotted on my calendar as well

e_by_s = Entry.where("emp_id = ?", 'BILL').pluck(:leave_start)
e_by_e = Entry.where("emp_id = ?", 'BILL').pluck(:leave_end)
r_j = e_by_s.to_s
r_g = e_by_e.to_s
p_g = r_j.to_date
t_g = r_g.to_date
days_between = p_g.upto(t_g) { |date| puts date }
@entry_by_all_dates = days_between.merge(days_between) {|key, oldval, newval| newval }
@date = params[:date] ? Date.parse(params[:date]) : Date.today
respond_to do |format|
format.html # index.html.haml
format.json { render :json => @entry }
end
end

举个例子: 我的 leave_start 日期是 2015-05-25

我的 leave_end 日期是 2015-05-29

两个日期之间的天数是这样的

离开_开始 2015-05-25

2015-05-262015-05-272015-05-28

离开 2015-05-29

额外信息

这是我的日历助手

 module CalendarHelper
def calendar(date = Date.today, &block)
Calendar.new(self, date, block).table
end

class Calendar < Struct.new(:view, :date, :callback)
HEADER = %w[Sun Mon Tues Wed Thur Fri Sat]
START_DAY = :sunday

delegate :content_tag, to: :view

def table
content_tag :table, class: "calendar" do
header + week_rows
end
end

def header
content_tag :tr do
HEADER.map { |day| content_tag :th, day }.join.html_safe
end
end

def week_rows
weeks.map do |week|
content_tag :tr do
week.map { |day| day_cell(day) }.join.html_safe
end
end.join.html_safe
end

def day_cell(day)
content_tag :td, view.capture(day, &callback), class: day_classes(day)
end

def day_classes(day)
classes = []
classes << "today" if day == Date.today
classes << "notmonth" if day.month != date.month
classes.empty? ? nil : classes.join(" ")
end

def weeks
first = date.beginning_of_month.beginning_of_week(START_DAY)
last = date.end_of_month.end_of_week(START_DAY)
(first..last).to_a.in_groups_of(7)
end

这也是我的日历 View

 #entry
%h2#month
= link_to "<", date: @date.prev_month
= @date.strftime("%B %Y")
= link_to ">", date: @date.next_month
= calendar @date do |date|
= date.day
- if @entry_by_date[date]
%ul
- @entry_by_date[date].each do |entry|
%li
%span.u= entry.emp_id
%ul
%span.u= entry.leave_start.strftime('%m/%d')
%span.u --
%span.u= entry.leave_end.strftime('%m/%d')
-elsif @entry_by_all_dates
%ul
- @entry_by_all_dates[date].each do |t|
%li
%span.u= t.emp_id
%ul
%span.u= t.leave_start.strftime('%m/%d')
%span.u --
%span.u= t.leave_end.strftime('%m/%d')

更新---

试过了

    e_by_s = Entry.where("emp_id = ?", 'BILL').pluck(:leave_start).first
e_by_e = Entry.where("emp_id = ?", 'BILL').pluck(:leave_end).first
r_j = e_by_s.to_s
r_g = e_by_e.to_s
p_g = r_j.to_date
t_g = r_g.to_date
@days_between = p_g..t_g
@entry_by_all_dates = (@days_between).map(&:to_s)
@date = params[:date] ? Date.parse(params[:date]) : Date.today
respond_to do |format|
format.html # index.html.haml
format.json { render :json => @entry }
end
end

在我看来

 #entry
%h2#month
= link_to "<", date: @date.prev_month
= @date.strftime("%B %Y")
= link_to ">", date: @date.next_month
= calendar @date do |date|
= date.day
- if @entry_by_date[date]
%ul
- @entry_by_date[date].each do |entry|
%li
%span.u= entry.emp_id
%ul
%span.u Type --
%span.u= entry.indirect_id
%ul
%span.u= entry.leave_start.strftime('%m/%d')
%span.u --
%span.u= entry.leave_end.strftime('%m/%d')
%ul
%span.u Shift --
%span.u= entry.am_pm
- @entry_by_all_dates[date].each do |t|
%li
%span.u= t.emp_id
%ul
%span.u= t.leave_start.strftime('%m/%d')
%span.u --
%span.u= t.leave_end.strftime('%m/%d')

出于某种奇怪的原因,我得到了这个错误...

     ActionView::Template::Error (can't convert Date into Integer):

我附上了我的日历的屏幕截图,它显示了开始日期和结束日期,我希望它们之间的日期也显​​示出来。

My calendar

最佳答案

require 'date'

start_date = Date.parse('2015-05-25')
end_date = Date.parse('2015-05-29')
date_range = start_date..end_date
(date_range).map(&:to_s)

这将输出一个日期范围在 (start_date..end_date) 之间的数组,如下所示

例子:

["2015-05-25", "2015-05-26", "2015-05-27", "2015-05-28", "2015-05-29"]

关于ruby-on-rails - 我希望能够在 Rails 应用程序的日历上标记开始日期和结束日期之间的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30326513/

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