jeudi 24 mars 2016

Rails session prevent navigation

In my controller I save the form data into session when user post the request to create new Invoice. But once in invoice page, I cant navigate away to another page because the session still not cleared. Following were my code.

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

Now the issues, if i cleared the session with #session[:pending_invoice] = nil, user wont be able to refresh the page. It will cause an error. How can I resolve this kind of issue? Thanks!!

Aucun commentaire:

Enregistrer un commentaire