设计注册一个用户注册,但需要两个不同的注册路径

时间:2019-07-25 14:29:50

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

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

<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>

Route.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', }

注册controller.rb

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

如果我单击业务按钮,我会考虑使用Java脚本或控制器之类的东西,那么路由将知道并将公司字段添加到new.html.erb中,而是决定选择两个新的company.html.erb中的哪一个< / p>

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

1 个答案:

答案 0 :(得分:0)

例如,在注册控制器中,根据@for_company的存在来设置标志变量params[:for_company]

class RegistrationsController < Devise::RegistrationsController
  def new
    @for_company = params[:for_company].present?
  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字段。

相关问题