我有一个考试的starts_at字段,我想要做的是,如果考试开始之前当前日期是1天或更长时间,那么我想重定向到其他地方。如果当前日期与考试开始时的日期相同_我想重定向到考试页面....现在我只想让if语句正确,我稍后会重新定位。
这是我的控制器。
学生会话控制器
class StudentSessionsController < ApplicationController
before_action :set_student_session
before_filter :config_opentok, except: :update
before_action :try_authenticate_user!, except: :mobile
before_action :check_compatability, except: :mobile
def show
@session = @student_session.session
@session_id = @session.session_id
@token = @opentok.generate_token @session_id, :data => "#{@student_session.id}"
# If Time.now is 1 or more days before exam starts_at show message
if (@session.exam.starts_at =< Time.now)
render :text => "OK"
else
render :text => "Not ok"
end
if @student_session.student.present?
#UserMailer.mobile_link(current_user.email, current_user.name, @student_session).deliver
else
#UserMailer.mobile_link(@student_session.email, @student_session.email, @student_session).deliver
end
ua = UserAgent.parse(request.user_agent)
@student_session.operating_system = ua.os
@student_session.browser = ua.browser
@student_session.browser_version = ua.version.to_s
@student_session.save
render layout: "application_no_header"
end
def mobile
@session = @student_session.session
@session_id = @session.session_id
@token = @opentok.generate_token @session_id, :data => "#{@student_session.id}_mobile"
render layout: false
end
def update
respond_to do |format|
if @student_session.update(student_session_params)
format.js
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_student_session
@student_session = StudentSession.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def student_session_params
params.require(:student_session).permit(:session_status, :publisher_status, :proctor_status, :mobile_status)
end
def config_opentok
@opentok ||= OpenTok::OpenTok.new APP_CONFIG['opentok']['api_key'], APP_CONFIG['opentok']['secret']
end
def try_authenticate_user!
if @student_session.student.present?
authenticate_user!
end
end
def check_compatability
user_agent = UserAgent.parse(request.user_agent)
# http://tokbox.com/opentok/requirements/
unless (user_agent.browser == 'Chrome' and user_agent.version.to_a.first >= 23) or
(user_agent.browser == 'Firefox' and user_agent.version.to_a.first >= 22)
redirect_to '/browser'
end
end
end
答案 0 :(得分:1)
if ((@session.exam.starts_at - Time.now).to_i / 1.day) >= 1
render :text => "OK"
else
render :text => "Not ok"
end
答案 1 :(得分:1)
当你使用rails时,你可以做到
if (Time.now >= (@session.exam.starts_at - 1.day) )
render :text => "OK"
else
render :text => "Not ok"
end
答案 2 :(得分:1)
我建议您使用:
if @session.exam.starts_at.to_date == Date.today
# go to exam
elsif @session.exam.starts_at.to_date < Date.today
# go to place before the exam
else
# go to place after the exam
end