[英]Allow user to create and manage other users
我有一個名為Account {id,名稱,地址,電子郵件,密碼等...}的用戶模型,並且我希望能夠允許該用戶創建和管理其他用戶。 我創建了一個關系模型,{manager_id,care_id,關系等...}來跟蹤用戶與其他受管用戶之間的關系。 基本上,用戶has_many relationships through: :relationships, source: "care_id"
具有has_many relationships through: :relationships, source: "care_id"
。 據我了解,在建立關系之前,必須先創建一個新用戶,以獲得一個新的用戶ID。 普通用戶注冊后將需要驗證電子郵件,密碼等……但是,對於受管用戶,您只需要一個名稱,他們便可以稍后再編輯其他內容。
實現此類功能的最佳/正確方法是什么? 我需要一個新的控制器嗎? 還是可以將新操作添加到默認的用戶控制器? 我如何在沒有用戶模型的常規驗證的情況下創建新用戶(名稱,電子郵件,密碼不能為空,必須具有一定的長度等)。 如何使用正確的用戶ID自動創建關系。 在此先感謝您的幫助和/或為我指明了正確的方向。
這個想法是這樣的:用戶可以注冊並管理他的帳戶。 然后,他可以創建一個“護理”帳戶,該帳戶基本上只需要一個名稱就可以開始使用,並且不需要其他驗證,因為子帳戶將不會使用電子郵件/密碼登錄。 如果子帳戶以后想要訪問,則父帳戶可以為其設置電子郵件/密碼,然后它將成為完整帳戶。 此父帳戶經理也可以邀請另一個帳戶(可能是配偶)充當此子帳戶的另一個經理。
更新:更多信息:
帳戶模型
class Account < ActiveRecord::Base
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX},
uniqueness: { case_sensitive: false },
length: { maximum: 32 },
unless: :child?
has_many :relationships, foreign_key: "manager_id", dependent: :destroy
has_many :cares, through: :relationships, source: :care
has_many :reverse_relationships, foreign_key: "care_id", class_name: "Relationship", dependent: :destroy
has_many :managers, through: :reverse_relationships, source: :manager
...
def child?
false
end
class CareUser < Account
def child?
true
end
end
關系模型
class Relationship < ActiveRecord::Base
attr_accessible :care_id, :manager_id, :type
belongs_to :manager, class_name: "Account"
belongs_to :care, class_name: "Account"
validates :manager_id, presence: true
validates :care_id, presence: true
end
護理主管
class CaresController < ApplicationController
def index
@cares = current_user.cares
end
def new
@user = CareUser.new
@care = Relationship.new
end
def create
@user = CareUser.new(params[:first_name])
logger.info @user.inspect
if @user.save(false)
@care = current_user.relationships.build(current_user.id, @user.id, params[:relation])
if @care.save
flash[:success] = "New care has been successfully added."
else
flash[:error] = "Failed"
render action: new
end
else
flash[:error] = "Failed adding user"
render action: new
end
end
def show
end
def edit
end
def update
end
def destroy
end
end
當@user = CareUser.new
我得到uninitialized constant CaresController::CareUser
,子類...但如果將其設置為父類的@user = Account.new
,則不會顯示任何錯誤。
提交表格cares / new.html.erb
不確定建立表單的正確方法,但是由於提交了兩個模型,因此我使用form_tag
而不是form_for
。 目前,我似乎無法正確提交表單,不確定問題出在表單,模型還是控制器。
<div class="row">
<div class="span6 offset3">
<h1>New care</h1>
<%= form_tag("/cares", method: :post) do %>
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@user.errors.count, "errors") %>
</div>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% if @care.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@care.errors.count, "errors") %>
</div>
<ul>
<% @care.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= label_tag(:first_name, "First name:") %>
<%= text_field_tag(:first_name) %>
<%= label_tag(:relation, "Relationship") %>
<%= select_tag(:relation, options_for_select([ ["Son", "1"], ["Daughter", "2"], ["Father", "3"], ["Mother", "4"], ["Grandson", "5"], ["Granddaughter", "6"], ["Grandfather", "7"], ["Grandmother", "8"] ])) %>
<br>
<%= submit_tag("Create care account") %>
<% end %>
</div>
</div>
根據您的要求
I have a user model {id, name, address, email, password, etc ... }, and I want to be able to allow that user to create and manage other users.
如果您只想跟蹤用戶的管理者是誰,則解決方案是在用戶表本身中具有“ manager_id ”列。 如果manager_id為NULL,則用戶本人是最高經理,否則為相應的經理id。
如果需要存儲有關該關系的更多詳細信息,則需要創建一個分離關聯表user_managers並說
用戶has_many:managers,通過user_mangers。
在這種情況下,您不必總是先創建用戶記錄。 每當您創建新用戶時,我都假設您需要為該用戶分配一個管理員。
manager = User.first
user = User.new
user_manager = manager.user_managers.new
user_manager.student = user
user.save!
user_managers表結構:
id, user_id, student_id
您可以創建此結構來管理具有無限級別的用戶層次結構:
class User < ActiveRecord::Base
has_many :user_relations, :dependent => :destroy
has_many :subordinates, :through => :user_relations
has_many :inverse_user_relations, :class_name => "UserRelation", :foreign_key => "subordinate_id"
has_many :superiors, :through => :inverse_user_relations, :source => :user
...
end
class UserRelation < ActiveRecord::Base
belongs_to :user
belongs_to :subordinate, :class_name => "User"
end
manager.subordinates = [subordinate_one, subordinate_two]
subordinate_one.superiors #manager user
manager.subordinates.size #2 users
admin.subordinates << manager
manager.superiors #admin user
admin.subordinates.size #1 user
然后,要管理用戶權限和驗證,可以結合使用cancan gem和用戶層次結構來執行與每種用戶類型相關的驗證。
class User < ActiveRecord::Base
validates :user_name, :presence => true, :uniqueness => true
#and other common validations
end
class Subordinate < User
#specific validations for this user type
end
class OtherUser < User
#specific validations for this user type
end
然后,在UsersController中,您可以創建類似以下內容的代碼:
def create
@user = Subordinate.new(params[:user])
@user.save
end
要么
def create
@user = OtherType.new(params[:user])
@user.save
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.