如何编写ruby代码来使用day = Date.today.wday方法显示“day”?

zbq4xfa0  于 9个月前  发布在  Ruby
关注(0)|答案(1)|浏览(96)

我正在做我的Ruby作业从学校。我需要使一天(星期日,星期一等)出现在日历应用程序。下面是原始代码中的问题,我收到;

class CalendarsController < ApplicationController
  def index
    getWeek
    @plan = Plan.new
  end

  def create
    Plan.create(plan_params)
    redirect_to action: :index
  end

  private

  def plan_params
    params.require(:calendars).permit(:date, :plan)
  end

  def getWeek
    wdays = ['(Sun)','(Mon)','(Tue)','(Wed)','(Thu)','(Fri)','(Sat)']

    @todays_date = Date.today
   
    @week_days = []

    plans = Plan.where(date: @todays_date..@todays_date + 6)

    7.times do |x|
      today_plans = []
      plans.each do |plan|
        today_plans.push(plan.plan) if plan.date == @todays_date + x
      end
      days = { :month => (@todays_date + x).month, :date => (@todays_date+x).day, :plans => today_plans}
      @week_days.push(days)
    end

  end
end

字符串
我试着写了如下代码:

@todays_date = Date.today.wday


我希望看到日期(Sun,Mon等)出现在应用程序上

x759pob2

x759pob21#

今天是星期三。当你想把一个日期转换成星期几的字符串表示时,你可以像这样使用Date#strftime

Date.today.strftime("%A")
#=> "Wednesday"
Date.today.strftime("%a")
#=> "Wed"

Date.today.strftime("(%a)")
#=> "(Wed)"

字符串

相关问题