2014-06-30 36 views
1

我有一个考试starts_at字段,我想要做的是,如果当前日期在考试starts_at前1天或更多,我想重定向到其他地方。如果当前日期与考试开始的同一天,我想重定向到考试页面......现在我只想让if语句正确,稍后我会重定向。如果当前时间比starts_at早1天或更多天

这是我的控制器。

学生会话控制器

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 

回答

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 
1
if ((@session.exam.starts_at - Time.now).to_i/1.day) >= 1 
     render :text => "OK" 
else 
     render :text => "Not ok" 
end 
1

当你正在使用的轨道,你可以做

if (Time.now >= (@session.exam.starts_at - 1.day)) 
    render :text => "OK" 
else 
    render :text => "Not ok" 
end 
+0

错误:电话回报和/或重定向在这个动作中多次编辑。 – Elton

+0

我摆脱了渲染布局:“application_no_header” – Elton

相关问题