2016-03-25 19 views
0

私のコントローラでは、新しい請求書の作成依頼を投稿するときに、フォームデータをセッションに保存します。しかし、請求書ページでは、セッションがまだクリアされていないため、別のページに移動することはできません。以下は私のコードです。私は#session[:pending_invoice] = nilsessionをクリアした場合Railsセッションがナビゲーションを妨げる

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 

今の問題は、ユーザーが文句を言わないページを更新することができます。エラーが発生します。このような問題を解決するにはどうすればよいですか?ありがとう!!

答えて

0

あなたのスクリプトから何が起こっているのか分かりません。申し訳ありませんが単純な答えではありません。

コントローラアクションは通常POSTリクエストを処理せず、フォームを描画するget要求を処理します。createアクションはフォームデータを含むPOSTを処理します。

POST中ではなくフォームをレンダリングする前にサインインを処理することをお勧めします。私のアプリケーションで
アプリケーションコントローラは、これはチュートリアル私は心からそれは非常に徹底的にサインインし、セッション管理をカバーし、お勧めしますby Michael Hartl

に基づいてライン 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 

を持っています。

最高の運

関連する問題