首页 > 解决方案 > 设计注册一个用户注册,但需要两个不同的注册路径

问题描述

由于我为这个项目工作了一段时间,因为这个项目不在范围内,直到昨天,客户和项目经理改变主意,因为他们想要两个不同的注册,因为以前是一个单一的注册,但现在想要更改两个,但我认为它可能使用相同的一个用户,但不同的路径

<div class="row">
  <div class="col-md-4 col-md-offset-4">
    <h2 class="text-center">Sign up</h2>

  </div>
  <div class="col-sm-offset-4 col-sm-4 margin-button-bottom">
    <div class="col-sm-12 text center">
      <div class="inner-addon right-addon">
        <i class="custom custom-icon-arrow-right"></i>
        <%= link_to "I want hire Equipment", new_user_registration_path, class: "btn btn-black-free-account btn-lg btn-block", role:"button" %>
      </div>
    </div>
  </div>

  <div class="col-sm-offset-4 col-sm-4  margin-button-bottom">
    <div class="col-sm-12 text center">
      <div class="inner-addon right-addon">
        <i class="custom custom-icon-arrow-right"></i>
        <p><%= link_to "I represent a business with equipment available for hire", new_user_registration_path, class: "btn btn-black-free-account btn-lg btn-block", role:"button" %></p>
      </div>
    </div>
  </div>
  <br/>

因此,如果此按钮“我代表一家可租用设备的企业”显示与用户相同的表单,但添加字段为公司

这里有两种不同的

设计注册 new.html.erb

<div class="row">
  <div class="col-md-4 col-md-offset-4">
    <h2 class="text-center">Sign up</h2>
    <br />

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= render 'shared/regmessage' %>

    <div class="form-group">
      <%= f.text_field :fullname, autofocus: true, placeholder: "Full name", class: "form-control", autocomplete: "fullname" %>
    </div>

    <div class="form-group">
      <%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control", autocomplete: "email" %>
    </div>

    <div class="form-group">
      <% if @minimum_password_length %>
      <em>(
        <%= @minimum_password_length %> characters minimum)</em>
      <% end %><br />
      <%= f.password_field :password, placeholder: "Password", class: "form-control", autocomplete: "new-password" %>
    </div>

    <div class="actions">
      <%= f.submit "Sign up", class: "btn btn-black-free-account btn-block" %>
    </div>
    <% end %>

    <hr />
    <%= link_to user_facebook_omniauth_authorize_path, class: "btn btn-block btn-social btn-facebook" do %>
      <span class="fa fa-facebook"></span> Sign in with Facebook
    <% end %>
    <hr />
    <%= link_to user_google_oauth2_omniauth_authorize_path, class: "btn btn-block btn-social btn-google" do %>
      <span class="fa fa-google"></span> Sign in with Google
    <% end %>
  <br/>
  </div>
</div>

与上面相同,但在 new_company.html.erb 中添加的是公司字段

<div class="row">
  <div class="col-md-4 col-md-offset-4">
    <h2 class="text-center">Sign up</h2>
    <br />

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= render 'shared/regmessage' %>

    <div class="form-group">
      <%= f.text_field :company, autofocus: true, placeholder: "Company Name", class: "form-control", autocomplete: "Company-name" %>
    </div>

    <div class="form-group">
      <%= f.text_field :fullname, autofocus: true, placeholder: "Full name", class: "form-control", autocomplete: "fullname" %>
    </div>


    <div class="form-group">
      <%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control", autocomplete: "email" %>
    </div>

    <div class="form-group">
      <% if @minimum_password_length %>
      <em>(
        <%= @minimum_password_length %> characters minimum)</em>
      <% end %><br />
      <%= f.password_field :password, placeholder: "Password", class: "form-control", autocomplete: "new-password" %>
    </div>

    <div class="actions">
      <%= f.submit "Sign up", class: "btn btn-black-free-account btn-block" %>
    </div>
    <% end %>

    <br/>
    <%= link_to user_facebook_omniauth_authorize_path, class: "btn btn-block btn-social btn-facebook" do %>
      <span class="fa fa-facebook"></span> Sign in with Facebook
    <% end %>
    <br/>
    <%= link_to user_google_oauth2_omniauth_authorize_path, class: "btn btn-block btn-social btn-google" do %>
      <span class="fa fa-google"></span> Sign in with Google
    <% end %>
  <br/>


  </div>
</div>

路由.rb

  devise_for  :users,
              path: '',
              path_names: {sign_in: 'login', sign_out: 'logout', edit: 'profile', sign_up: 'registration'},
              :controllers => {omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations', }

注册控制器.rb

class RegistrationsController < Devise::RegistrationsController
  protected
    def update_resource(resource, params)
      resource.update_without_password(params)
    end
end

如果我单击业务按钮,我正在考虑类似 java-script 或控制器的东西,然后路线将知道并将公司字段添加到 new.html.erb 中,而不是决定去两个新的和新的 company.html.erb 中的哪一个

可能是 new.html.erb 中的新 ID,还是需要在路由或控制器上配置?

标签: ruby-on-railsrubydeviseruby-on-rails-5

解决方案


在您的注册控制器中设置一个标志变量@for_company,例如基于params[:for_company]存在:

class RegistrationsController < Devise::RegistrationsController
  def new
    @for_company = params[:for_company].present?
    super
  end
end

在您的注册页面中,只需将for_company: true参数添加到公司注册的链接:

<%= link_to "I represent a business with equipment available for hire", new_user_registration_path(for_company: true), class: "..." %>

然后只显示:company字段以防万一@for_company


推荐阅读