I am usin开发者_JAVA技巧g Devise for authentication.
I'm using it for signup and the editing of their account. I need the ability to add "sub" users to each account. I can get it to work if I remove :registerable from the User Model, but by doing this it breaks edit_user_registration_path.
What I need todo is:
Allow new users to sign up.
Allow existing customers to add "Sub Users" to their account.
I think I need to use a self-referencing relationship to create the account owner.
Heres the code I have at the moment
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
(If I remove :registerable I can create new users using User 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
Users/new
<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 %>
You can add a :has_many :belongs to relationship inside your User. something like
class User
belongs_to :parent, :class_name => 'User'
has_many :children, :class_name => 'User'
...
end
and in your controller add a reference to your parent user.
class UsersController < ApplicationController
def new
@user = User.new
@user.parent_id = params[:parent_id]
respond_to do |format|
end
end
精彩评论