设计注册一个用户注册,但需要两个不同的注册路径
devise Register one users sign up, but need two different register paths
因为我已经为这个项目工作了一段时间,因为这个项目不在范围内,直到昨天,因为客户和项目经理改变了主意,因为他们想要两个不同的注册,因为它之前是一个单一的注册但是现在想换两个,但我认为可以使用同一个用户,但路径不同
<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/>
因此,如果此按钮 "I represent a business with equipment available for hire" 显示与用户相同的表单,但添加字段是公司
这里有两个不同的
设计寄存器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
可能是 new.html.erb 中的新 ID 或者需要在路由或控制器上配置?
在您的注册控制器中设置一个标志变量 @for_company
例如基于 params[:for_company]
存在:
class RegistrationsController < Devise::RegistrationsController
def new
@for_company = params[:for_company].present?
super
end
end
然后在您的注册页面中,只需将 for_company: true
参数添加到公司注册的 link 中:
<%= link_to "I represent a business with equipment available for hire", new_user_registration_path(for_company: true), class: "..." %>
如果 @for_company
为真,则只显示 :company
字段。
因为我已经为这个项目工作了一段时间,因为这个项目不在范围内,直到昨天,因为客户和项目经理改变了主意,因为他们想要两个不同的注册,因为它之前是一个单一的注册但是现在想换两个,但我认为可以使用同一个用户,但路径不同
<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/>
因此,如果此按钮 "I represent a business with equipment available for hire" 显示与用户相同的表单,但添加字段是公司
这里有两个不同的
设计寄存器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
可能是 new.html.erb 中的新 ID 或者需要在路由或控制器上配置?
在您的注册控制器中设置一个标志变量 @for_company
例如基于 params[:for_company]
存在:
class RegistrationsController < Devise::RegistrationsController
def new
@for_company = params[:for_company].present?
super
end
end
然后在您的注册页面中,只需将 for_company: true
参数添加到公司注册的 link 中:
<%= link_to "I represent a business with equipment available for hire", new_user_registration_path(for_company: true), class: "..." %>
如果 @for_company
为真,则只显示 :company
字段。