想要为使用 devise gem 构建的注册表构建一个多步骤表单 [英] Want to build a multiple step form for registration form built with devise gem

查看:27
本文介绍了想要为使用 devise gem 构建的注册表构建一个多步骤表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 devise 生成注册/注册表格.但是我的注册表单有很多字段,我想用它们来制作多步表单.第一步将是接受电子邮件和密码.当用户输入时,他们可以继续填写其他表格.

 

注册

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|%><%= devise_error_messages!%><div class="field"><%= f.label :first_name %><br/><%= f.text_field :first_name, autofocus: true %>

<div class="field"><%= f.label :last_name %><br/><%= f.text_field :last_name %></br>

<div class="field"><%= f.label :birth_date %></br><%= f.date_select :birth_date, :start_year=>1905,:end_year=>2015 %>

<div class="field"><%= f.label :city%></br><%= f.text_field :city %>

<div class="field"><%= f.label :address %></br><%= f.text_area :地址%>

<div class="field"><%= f.label :country %></br><%= f.select(:country, [['印度', '印度'], ['美国', '美国']]) %>

<div class="field"><%= f.label :zip_code %></br><%= f.text_field :zip_code %>

<div class="field"><%= f.label :gender %></br><span class="option">男</span><%= f.radio_button :gender, "m" %><span class="option">Female</span><%= f.radio_button :gender, "f" %></br>

<div class="field"><%= f.label :mobile_no %></br><%= f.telephone_field :mobile_no %>

<div class="field"><%= f.label :website %></br><%= f.url_field :website %>

<div class="field"><%= f.label :email %><br/><%= f.email_field :email %>

<div class="field"><%= f.label :password %><% if @validatable %><em>(<%= @minimum_password_length %> 最少字符数)</em><%结束%><br/><%= f.password_field :password, autocomplete: "off" %>

<div class="field"><%= f.label :password_confirmation %><br/><%= f.password_field :password_confirmation, autocomplete: "off" %>

<div class="field"><%= f.label :Launguages_Known %><br/><%= f.text_field :language %>

<div class="field"><%= f.label :skills %><br/><%= f.text_field :skills %>

<div class="field"><%= f.label :passion %><br/><%= f.text_field :passion %>

<div class="field"><%= f.label :connecting_conditions %><br/><%= f.text_area :connect_pref %>

<div class="actions"><%= f.submit "注册" %>

<%结束%>

这个表格很长,所以我想把它做成多步.同样在成功填写此表单后,用户应被重定向到特定页面.

需要帮助生成注册页面的多步表单并在成功注册后重定向到状态页面.

解决方案

如果您想分步提交表单,请按照以下步骤进行:

https://github.com/aasm/aasm

假设您想分三步提交表单,如下所示:

profile_details

地址_详细信息

技术细节

首先,您需要像这样在模态中定义横切状态:

 aasm 做状态 :loaded, :initial =>真的状态:profile_details状态:address_details状态:技术细节事件 :fill_basic_info 做转换:从=>:loaded, :to =>:profile_details, :guard =>Proc.new { |o|o.有效?}结尾事件:fill_residential do转换:从=>:profile_details, :to =>:详细地址结尾事件 :fill_skills 做转换:从=>:address_details , :to =>:技术细节结尾事件:previous_step 做转换:从=>:technical_details, :to =>:详细地址转换:从=>:address_details , :to =>:profile_details转换:从=>:profile_details, :to =>:加载结尾结尾def change_ad_next_state案例 self.aasm_state当加载"fill_basic_info当profile_details"填充_住宅当address_details"填充技能结尾结尾

在您的控制器上:

def 创建@user = User.new(user_params)如果@user.select_tenant &&@user.saveredirect_to edit_user_path(@user)别的@user.previous_step呈现新"结尾结尾定义更新@user = User.find params[:id]如果@user.change_ad_next_state &&@user.update_attributes(advertisement_params)重定向到 some_path_here 并返回别的@user.previous_step渲染编辑"并返回结尾结尾def previous_step@user = User.find(params[:id])如果 !@user.loaded?&&@user.previous_step &&@user.saveredirect_to edit_user_path(@user)别的重定向到:返回结尾结尾私人的定义用户参数params.require(:user).permit(:first_name,:last_name,:email,:password 等等....)结尾

如果你没有定义,也为所有方法定义路由.

I used devise to generate sign-up/registration form. But my registration form have many fields which I want to use to make multiple step form. The first steps will is to accept email n password. As user enter it, they can proceed to fill other form.

    <h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>

    <div class="field">
      <%= f.label :first_name %><br />
      <%= f.text_field :first_name, autofocus: true %>
    </div>

    <div class="field">
      <%= f.label :last_name %><br />
      <%= f.text_field :last_name %></br>
    </div>

    <div class="field">
      <%= f.label :birth_date %></br>

      <%= f.date_select :birth_date, :start_year=>1905,:end_year=>2015 %>
    </div>

    <div class="field">
      <%= f.label :city %></br>
      <%= f.text_field :city %>
    </div>

    <div class="field">
      <%= f.label :address %></br>
      <%= f.text_area :address %>
    </div>

    <div class="field">

        <%= f.label :country %></br>
        <%= f.select(:country, [['India', 'India'], ['USA', 'USA']]) %>
    </div>

    <div class="field">
      <%= f.label :zip_code %></br>
      <%= f.text_field :zip_code %>
    </div>

    <div class="field">
      <%= f.label :gender %></br>
      <span class="option">Male</span><%= f.radio_button :gender, "m" %>
      <span class="option">Female</span><%= f.radio_button :gender, "f" %></br>
    </div>

    <div class="field">
      <%= f.label :mobile_no %></br>
      <%= f.telephone_field :mobile_no %>
    </div>

    <div class="field">
      <%= f.label :website %></br>
      <%= f.url_field :website %>
    </div>

    <div class="field">
      <%= f.label :email %><br />
      <%= f.email_field :email %>
    </div>

    <div class="field">
      <%= f.label :password %>
      <% if @validatable %>
          <em>(<%= @minimum_password_length %> characters minimum)</em>
      <% end %><br />
      <%= f.password_field :password, autocomplete: "off" %>
    </div>

    <div class="field">
      <%= f.label :password_confirmation %><br />
      <%= f.password_field :password_confirmation, autocomplete: "off" %>
    </div>

    <div class="field">
      <%= f.label :Launguages_Known %><br />
      <%= f.text_field :language %>
    </div>

    <div class="field">
      <%= f.label :skills %><br />
      <%= f.text_field :skills %>
    </div>

    <div class="field">
      <%= f.label :passion %><br />
      <%= f.text_field :passion %>
    </div>

    <div class="field">
      <%= f.label :connecting_conditions %><br />
      <%= f.text_area :connect_pref %>
    </div>


    <div class="actions">
      <%= f.submit "Sign up" %>
    </div>
<% end %>

This form is very long so, I want to make it multistep. Also after successful filling this form, user should be redirected to a specific page.

Need help to generate multistep form for registration page and redirection to status page after successful signup.

解决方案

If you want to submit your form in steps so please go through with this :

https://github.com/aasm/aasm

suppose you want to submit your form in three steps like this :

profile_details

address_details

technical_details

First you need to define transection states at your modal like this :

 aasm do
    state :loaded, :initial => true
    state :profile_details
    state :address_details
    state :technical_details

  event :fill_basic_info do
    transitions :from => :loaded, :to => :profile_details, :guard => Proc.new { |o|
    o.valid?
  }
  end

  event :fill_residential do
    transitions :from => :profile_details, :to => :address_details
  end

  event :fill_skills do
    transitions :from => :address_details , :to => :technical_details
  end

  event :previous_step do
    transitions :from => :technical_details, :to => :address_details
    transitions :from => :address_details , :to => :profile_details
    transitions :from => :profile_details, :to => :loaded
  end
 end

def change_ad_next_state
   case self.aasm_state
   when "loaded"
    fill_basic_info
   when "profile_details"
    fill_residential
   when "address_details"
    fill_skills
   end
 end

At your controller :

def create
  @user = User.new(user_params)

 if @user.select_tenant && @user.save
   redirect_to edit_user_path(@user)
 else
   @user.previous_step
   render "new"
 end
end

def update
  @user = User.find params[:id]
  if @user.change_ad_next_state &&    @user.update_attributes(advertisement_params)
    redirect_to some_path_here and return
  else
    @user.previous_step
    render "edit"  and return
  end
end

def previous_step
  @user = User.find(params[:id])
  if !@user.loaded? && @user.previous_step && @user.save
    redirect_to edit_user_path(@user)
  else
    redirect_to :back
  end
end

private 

 def user_params
   params.require(:user).permit(:first_name,:last_name,:email,:password  etc....)
 end

and also define routes for all the methods if you didn't define.

这篇关于想要为使用 devise gem 构建的注册表构建一个多步骤表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆