Rails会话阻止导航

时间:2016-03-25 01:25:50

标签: ruby-on-rails ruby ruby-on-rails-3

在我的控制器中,当用户发布创建新发票的请求时,我将表单数据保存到会话中。但是一旦在发票页面中,我无法导航到另一个页面,因为会话仍未清除。以下是我的代码。

def new

        @invoice = Invoice.new

        #post request from detail page to invoice along with variable from form
        if request.post?     

            @invoice.booking_date = params[:datepicker]
            @invoice.product_id = params["product-id"]
            @invoice.variants = params[:variant]

            #if user not signed in, user redirected to sign in page before    
            #continue to post invoice page with form data saved in session
            if !user_signed_in?
                session[:pending_invoice] = @invoice
                return redirect_to new_user_session_path

            end

            #following for case where user already signed in before directed to 
            #invoice page
            set_extra_params_for_new
            @invoice_params = {}
            @contact_detail_params = {}

        else

            #this is when user are from sign in page with session present. This is 
            #the code that make the issues where I cant navigate away to other 
            #pages until I cleared the session with session[:pending_invoice] = 
            #nil.
            if (session[:pending_invoice].present?) && (session[:pending_invoice].instance_of? Invoice)

                @invoice = session[:pending_invoice]
                set_extra_params_for_new

                @invoice_params = {}

                @contact_detail_params = {}

            else
                #this code for params post request that comes from payment gateway. So  
                #all the data in invoice page is intact
                @invoice.booking_date = params[:invoice][:booking_date]
                @invoice.product_id = params[:invoice][:product_id]
                @invoice.coupon_id = params[:invoice][:coupon_id]
                @invoice.coupon_amounts = params[:invoice][:coupon_amounts]
                @coupon_amounts_with_user_currency = params[:invoice][:coupon_amounts_with_user_currency]
                @invoice.variants = params[:variant]
                set_extra_params_for_new

                @invoice_params = params[:invoice]

                #session[:pending_invoice] = nil

            end
        end

        set_invoice_currency_attributes(@invoice)

        gon.invoice = @invoice
        gon.real_total_with_currency = @invoice.real_total_with_currency
        gon.real_total = @invoice.real_total


    end

现在问题是,如果我使用session清除#session[:pending_invoice] = nil,用户将无法刷新页面。它会导致错误。我该如何解决这类问题?谢谢!

1 个答案:

答案 0 :(得分:0)

我无法弄清楚你的脚本发生了什么,抱歉这不是简单的答案。

new控制器操作通常不处理POST请求,它处理一个get请求以呈现要填写的表单。create操作处理包含表单数据的POST。

我建议您在发布任何表格之前处理登录 在我的应用程序中应用程序控制器具有该行 before_action :require_signin

def require_signin
    unless signed_in?
        store_location
        flash[:danger] = "You must be signed in to access this section"
        redirect_to signin_url # halts request cycle
    end
end

这是基于教程by Michael Hartl

我衷心推荐,它非常彻底地涵盖了登录和会话管理。

祝你好运