2012-06-11 38 views
1

我需要一個表單頁面,每個表單提交到同一個索引頁面。在每次提交時,根據輸入字段的驗證方式顯示不同的視圖。如何在提交到相同頁面時添加到導軌會話?

控制器:

class FormsController < ApplicationController 
    include FormsHelper 

    def index 
     #if we know what view to show, show it, else show first view in the flow 
     if(!session.has_key?(:flow_page)) 
      set_flow_page 
     end 

     # if form was submitted, I want to add this pages submit data to what I already have in session[:quote] 
     if(params.has_key?(:form)) 
      temp = params[:form] 
      form = session[:quote] 
      form.merge(temp) 
      #session[:quote].deep_merge!(session[:temp]) 
     end 

     # other stuff 
     if params[:back] == "Back" && params[:flow][:previous_page] != "refused" 
      session[:flow_page] = params[:flow][:previous_page] 
     end 

     if params[:next] == "Next" 
      session[:flow_page] = params[:flow][:next_page] 
     end 

    end 

end 

有人能告訴我如何從每個視圖到會話中添加表單數據[:報價]不會覆蓋時間整個塊?

回答

1

不知道你在做什麼,但是讓我們假設你想保留你的表單的歷史記錄,這些表單在提交時碰到同樣的方法。要使波紋管代碼正常工作,需要爲每個窗體添加帶有UID的隱藏窗體字段。名稱字段「form_name」

# init form cache, if not initiated before 
    session[:quote] = {} unless session.has_key(:quote) 

    # keys to be excluded 
    filter_out = [:form_name,:controller, :action] 

    # store to cache, with form UID/we filter out params we don't need 
    session[params[:form_name]] = params.dup.keep_if { |k,v| !filter_out.include?(k) } 
相關問題