我正在使用Devise进行身份验证。
我正在使用它注册和编辑他们的帐户。我需要能够添加“子”用户到每个帐户。如果我从用户模型中删除:registerable,我可以让它工作,但是这样做会破坏edit_user_registration_path。
我需要做的是:
允许新用户注册。
允许现有客户将“子用户”添加到其帐户。
我认为我需要使用自引用关系来创建帐户所有者。
这是我目前拥有的代码
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :location, :country, :job_title, :company
end(如果我删除:registerable,我可以使用用户CRUD创建新用户)
class UsersController < ApplicationController
def new
@user = User.new
respond_to do |format|
format.html
end
end
def create
@user = User.new(params[:user])
if @user.save
respond_to do |format|
format.html { redirect_to :action => :index }
end
else
respond_to do |format|
format.html { render :action => :new, :status => :unprocessable_entity }
end
end
end
end用户/新用户
<h2>Register User</h2>
<%= form_for(@user) do |f| %>
<%= f.error_messages %>
<p><%= f.label :email %><br />
<%= f.text_field :email %></p>
<p><%= f.label :password %></p>
<p><%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %></p>
<p><%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Register" %></p>
<% end %>发布于 2010-12-08 06:31:56
你可以在你的用户中添加一个:has_many :属于关系。就像这样
class User
belongs_to :parent, :class_name => 'User'
has_many :children, :class_name => 'User'
...
end并在您的控制器中添加对您的父用户的引用。
class UsersController < ApplicationController
def new
@user = User.new
@user.parent_id = params[:parent_id]
respond_to do |format|
end
endhttps://stackoverflow.com/questions/4379650
复制相似问题