2016-11-14 1 views
0

私はRails 4 Appオーダーモデルを使用しています。価格計算などに必要なデータをユーザーがフォームに記入します。 step_3の最後にサインインするように促され、支払いセクションにルーティングされますが、ユーザーが以前に記入した注文モデルからフォームデータを保存できます。このデータは注文フォームデータです。サインフォームデータをレールで保持したままフォームに記入してサインインする方法

主な問題点:を考案使ってプロセスを通じて半ば方法で記号の後にそれらをリダイレクト

  1. フォームデータを保存し、直前に支払いセクション

  2. Order.find_byセクションにルーティング、現在のユーザーに関連付け。注文は何によってわかりますか?

背景: Railsの4 PostgreSQLの 工夫宝石

class OrdersController < ApplicationController 
before_action :authenticate_user!, :except => [:step_1,:process_step1,:step_2a,:process_step_2a, :step_2b, :step_2c, :step_3] 

def step_1 
    @order = Order.find_by id: session[:order_id] 
    if @order.nil? 
    @order=Order.new 
    @order.save! 
    end 
end 

def process_step1 
order = Order.find_by id: session[:order_id] 
order.update_attributes(order_params) 
if (order.building == 'Residential') || (order.building == 'Commercial') 
    redirect_to step_2a_path 
elsif (order.building == 'Commercial + Parking') || (order.building == 'Residential + Parking') 
    redirect_to step_2b_path 
else 
    redirect_to step_2c_path 
    end 
end 

    def step_2a 
    @order = Order.find_by id: params[:session_id] 
end 

    def process_step_2a 
    order= Order.find_by status: 'cart' 
# , user_id: current_user.id 
order.update_attributes(order_params) 
if order.save 
    redirect_to step_3_path 
    end 
    end 

    def step_2b 
    @order= Order.find_by status:'cart' 
# , user_id: current_user.id 
    end 

    def process_step_2b 
    order= Order.find_by status: 'cart' 
# , user_id: current_user.id 
order.update_attributes(order_params) 
if order.save 
    redirect_to step_3_path 
end 
end 

    def step_2c 
    @order= Order.find_by status:'cart' 
    # , user_id: current_user.id 
    end 

    def process_step_2c 
    order= Order.find_by status: 'cart' 
    order.update_attributes(order_params) 
    if order.save 
    redirect_to step_3_path 
end 
    end 

    def step_3 
    @order= Order.find_by status:'cart' 
# , user_id: current_user.id 
    end 

def process_step_3 
    order= Order.find_by status: 'cart', user_id: current_user.id 
    order.update_attributes(order_params) 
    if order.save 
    redirect_to payment_path 
end 
end 

def payment 
    @order= Order.find_by status:'cart', user_id: current_user.id 
end 

答えて

0

まず第一に、あなたは本当にあなたのルートをリファクタリングする必要があります。その構造では、アプリケーションは指数関数的に大きなものをナビゲートするのがより混乱し、他の誰かがその上で作業したり、デバッグに役立つことは事実上不可能になります。詳細はRESTful routingのこのガイドをご覧ください。

はあなたの問題として、あなたは、一時的な記憶メカニズムは、レールの一つに自分のフォームデータを保存することができます(私はお勧めsession variable)彼らは、彼らが

0
  1. あり終えた後、それを再表示するにはログインしている間after_sign_in_path_forのコールバックです。これをApplicationControllerに追加できます。使用方法は、here

2,3です。セッション[:order_id]を引き続き使用してください。なぜfind_byのステータスを使用するのか分からない: 'cart'とOrder.find_by id:params [:session_id]。

個人的に私はこの方法をお勧めしません。オーダーIDをクッキーに保存された安全なランダム生成値にバインドするほうがよいと思います。

関連する問題