Rails在使用Devise注册后发布多步表单数据

时间:2013-03-14 22:06:52

标签: ruby-on-rails

我有一个两步形式,可以在会话中保存数据,并在用户登录时创建新记录。

如果新用户尚未注册,我希望他能够:

  1. 填写表格#1
  2. 填写表格#2
  3. 重定向到设计注册(new_registration_path
  4. 在注册/登录完成后从表单数据发布/创建记录
  5. 请参阅下面的代码( location_controller.rb ):

    开:“elsif @location.last_step?”,如果用户未登录但在登录后未保存数据,则会指示注册。

    有没有办法传递会话表单数据思想设计,以便在注册后发布/创建记录?

    提前致谢

    def new
      session[:location_params] ||= {}
        if current_user.nil?
          @location = Location.new(session[:location_params])
        else
          @location = current_user.locations.new(session[:location_params])
        end
    
      @location.current_step = session[:location_step]
    
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @location }
      end   
    end
    
    def create
        session[:location_params].deep_merge!(params[:location]) if params[:location]
        if current_user.nil?
          @location = Location.new(session[:location_params])
        else
          @location = current_user.locations.new(session[:location_params])
        end
        @location.current_step = session[:location_step]
        if @location.valid?   
          if params[:back_button]
            @location.previous_step
          elsif @location.last_step?
           @location.save
          else
            @location.next_step
          end
          session[:location_step] = @location.current_step
        end
    
        if @location.new_record?
          render "new"
        else
          session[:location_step] = session[:location_params] = nil
          flash[:notice] = "Trip saved!"
          redirect_to @location
        end
      end
    

1 个答案:

答案 0 :(得分:1)

我解决了它:

  1. 在locations.rb
  2. 中添加了store_form_data(@location) Session中的
  3. store_form_data函数(见下文)
  4. include SessionsHelper添加到a​​pplication_controller.rb
  5. 在application_controller.rb中添加了def after_sign_in_path_for(resource)函数(请参阅打击)
  6. 应用程序/助手/ session_helper.rb:

    module SessionsHelper
      def store_form_data(locations)
        session[:form_data] = locations
      end
    end
    

    应用程序/控制器/ application_controller.rb:

    class ApplicationController < ActionController::Base
      protect_from_forgery
      include SessionsHelper
    
      def after_sign_in_path_for(resource)
        if session[:form_data].present?
          @user = current_user
          @location = session[:form_data]
          @user.locations << @location
          session[:form_data] = nil
          flash[:notice] = 'Trip saved!'
          index_path
        else
          new_location_path
        end
      end
    end
    

    应用/控制器/ locations_controller.rb:

     def new
        #@location = Location.new
        session[:location_params] ||= {}
        session[:form_data] = nil
        if current_user.nil?
          @location = Location.new(session[:location_params])
        else
          @location = current_user.locations.new(session[:location_params])
        end
    
        @location.current_step = session[:location_step]
    
        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @location }
        end
      end
    
    def create
        session[:location_params].deep_merge!(params[:location]) if params[:location]
        if current_user.nil?
          @location = Location.new(session[:location_params])
        else
          @location = current_user.locations.new(session[:location_params])
        end
        @location.current_step = session[:location_step]
        if @location.valid?   
          if params[:back_button]
            @location.previous_step
          elsif @location.last_step?
            if current_user.nil?
              store_form_data(@location)
            end
           @location.save
          else
            @location.next_step
          end
          session[:location_step] = @location.current_step
        end
    
        if @location.new_record?
          render "new"
        else
          session[:location_step] = session[:location_params] = nil
          flash[:notice] = "Trip saved!"
          redirect_to @location
        end
      end