我有一个两步表单,它在会话中保存数据并假设用户已登录创建一个新记录。
如果新用户尚未注册,我希望他能够:
- 填写表格#1
- 填写表格#2
- 重定向到设计注册 (
new_registration_path
) - 注册/登录完成后从表单数据发布/创建记录
请参见下面的代码(location_controller.rb):
On:“ 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