我的网站上有两种类型的注册。
有两种与注册相关的模式
users(id, name, is_host, ...)
hosts(company_name, user_id, status, ...)
默认情况下,每个主机都是应用程序上的用户。当用户在网站上注册时,必须输入以下字段
当主机注册时,他必须输入以下内容
在将表单作为主机提交时,应该将数据保存在用户模型中,并将is_host
标志设置为1
(其他明智的方法是0
),然后将company_name
存储在hosts
模型中。
我做了什么?
registrations_controller.rb
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
<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
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
发布于 2015-09-19 20:13:03
您可以保留默认的"devise_for :users“,并具有默认的设计功能,并有第二个位于/host/user/的功能。
routes.rb
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结合起来,只修改主机的注册流即可。
https://stackoverflow.com/questions/27546414
复制相似问题