2014-06-30 84 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

相關問題