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

有人能告诉我如何将每个视图中的表单数据添加到 session[:quote] 中,而不会每次都覆盖整个块吗?

4

1 回答 1

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) }
于 2012-06-11T12:22:49.840 回答