【问题标题】:Sub-users and Devise子用户和设计
【发布时间】:2010-12-07 17:29:29
【问题描述】:

我正在使用 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 %>

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    您可以在用户内部添加一个 :has_many :belongs 关系。像

     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
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多