首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设计2注册流程

设计2注册流程
EN

Stack Overflow用户
提问于 2014-12-18 12:16:19
回答 1查看 746关注 0票数 0

我的网站上有两种类型的注册。

  1. 用户
  2. 主机

有两种与注册相关的模式

代码语言:javascript
运行
复制
users(id, name, is_host, ...)

hosts(company_name, user_id, status, ...)

默认情况下,每个主机都是应用程序上的用户。当用户在网站上注册时,必须输入以下字段

  1. 名字
  2. 电子邮件
  3. 密码
  4. 电话号码

当主机注册时,他必须输入以下内容

  1. 名字
  2. 电子邮件
  3. 密码
  4. 电话号码
  5. 公司名称

在将表单作为主机提交时,应该将数据保存在用户模型中,并将is_host标志设置为1 (其他明智的方法是0 ),然后将company_name存储在hosts模型中。

我做了什么?

registrations_controller.rb

代码语言:javascript
运行
复制
class RegistrationsController < Devise::RegistrationsController

  def new_host
    build_resource({})
    #set_minimum_password_length
    #yield resource if block_given?
    #respond_with self.resource
  end

  def create_host
    # build_resource(sign_up_params)
    # resource_saved = resource.save
    # yield resource if block_given?
    # if resource_saved
    #   if resource.active_for_authentication?
    #     set_flash_message :notice, :signed_up if is_flashing_format?
    #     sign_up(resource_name, resource)
    #     respond_with resource, location: after_sign_up_path_for(resource)
    #   else
    #     set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
    #     expire_data_after_sign_in!
    #     respond_with resource, location: after_inactive_sign_up_path_for(resource)
    #   end
    # else
    #   clean_up_passwords resource
    #   set_minimum_password_length
    #   respond_with resource
    # end

    puts "********************************************"
  end

  def update
    # For Rails 4
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
    # For Rails 3
    # account_update_params = params[:user]

    # required for settings form to submit when password is left blank
    if account_update_params[:password].blank?
      account_update_params.delete("password")
      account_update_params.delete("password_confirmation")
    end

    @user = User.find(current_user.id)
    if @user.update_attributes(account_update_params)
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
  end

  def update_password
    @user = User.find(current_user.id)
    if @user.update_with_password(account_update_params)
      # Sign in the user by passing validation in case their password changed
      # sign_in @user, :bypass => true
      redirect_to root_path
    else
      render "edit"
    end
  end

  protected

  def after_update_path_for(resource)
    edit_user_registration_path
  end

end

new_host.html.rb

代码语言:javascript
运行
复制
<h2>Sign up!!</h2>

<%= form_for(resource, as: resource_name, url: simaple_path) 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 %>
  </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="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "users/shared/links" %>

routes.rb

代码语言:javascript
运行
复制
devise_for :users, :controllers => { :registrations => "registrations" }

  devise_scope :user do
    match '/become_a_host' => 'registrations#new_host', :via => :get
    match '/create_host' => 'registrations#new_host', :via => :post, as: :simaple
    #get 'registrations#create_host', :via => :post
  end
EN

回答 1

Stack Overflow用户

发布于 2015-09-19 20:13:03

您可以保留默认的"devise_for :users“,并具有默认的设计功能,并有第二个位于/host/user/的功能。

routes.rb

代码语言:javascript
运行
复制
devise_for :users

namespace :host do
  devise_for :users do
    controller :registrations do
      get 'new' => :new_host
      post 'update' => :update
      post 'create' => :create_host
    end
  end
end

只需将其与您的自定义RegistrationsController结合起来,只修改主机的注册流即可。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27546414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档