ruby-on-rails - Rails 在使用 Devise 注册后发布多步骤表单数据

标签 ruby-on-rails

我有一个两步表单,它在 session 中保存数据并在假设用户已登录的情况下创建一条新记录。

如果新用户还没有注册,我希望他能够:

  1. 填写表格 #1
  2. 填写表格 #2
  3. 重定向到 Devise 注册(new_registration_path)
  4. 在注册/登录完成后从表单数据发布/创建记录

见下面的代码(location_controller.rb):

On: "elsif @location.last_step?",如果用户没有登录,它会直接注册,但登录后不保存数据。

有没有办法将 session 表单数据传递给 Devise,以便它在注册后发布/创建记录?

提前致谢

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. 在 locations.rb 中添加了 store_form_data(@location)
  2. SessionHelper 中的 store_form_data 函数(见下文)
  3. 添加include SessionsHelper到application_controller.rb
  4. 在 application_controller.rb 中添加了 def after_sign_in_path_for(resource) 函数(见下文)

app/helpers/session_helper.rb:

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

应用程序/ Controller /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

apps/controllers/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

关于ruby-on-rails - Rails 在使用 Devise 注册后发布多步骤表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15421119/

相关文章:

ruby-on-rails - 如何在 Rails 中为字段设置显示名称

ruby-on-rails - 使用 rspec、capybara 和 stripe 测试注册表单

html - Rails 5.1 CSS 背景图像 ID 未加载?

ruby-on-rails - 如何修复 Rails 中 pg_attribute 表的缓慢隐式查询

ruby-on-rails - PASSENGER/RVM 权限被拒绝 (13)

ruby-on-rails - 在 Rails 3 上将链式作用域作为参数传递?

ruby-on-rails - 如何在 rails/rack 应用程序上与 node.js 实例共享身份验证?

ruby-on-rails - 如何使rails发布的cookie跨子域工作

ruby-on-rails - 如何访问模型类中的类变量

ruby-on-rails - 使用 Oj JSON 解析器无法获得性能改进