同时使用帐户和用户表与Devise

Mar*_*rry 19 authentication ruby-on-rails devise ruby-on-rails-3

我正在使用Rails 3.1.0和Devise 1.4.8,对我们两个都是新手.

我想允许多个用户使用帐户.第一个注册用户创建帐户(可能是他们的公司),然后该用户可以添加更多用户.用户始终只链接到一个帐户.

我有用户和帐户表.缩写模型:

class User < ActiveRecord::Base
  belongs_to :account
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,
     :confirmable, :lockable, :timeoutable
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

class Account < ActiveRecord::Base
  has_many :users, :dependent => :destroy
  attr_accessible :name, :account_type
end
Run Code Online (Sandbox Code Playgroud)

问题是,当第一个用户注册时,如何创建帐户和用户?

  • 我是否需要修改/覆盖Devise registrations_controller,就像这里的答案一样?我无法弄清楚如何创建帐户然后将其传递给Devise以创建用户.

  • account_id已在User模型中.我应该将account_name和account_type添加到用户模型,如果未传入account_id,还要创建一个新的帐户记录?在这种情况下,我试图隐藏Devise中的帐户创建,但不确定这是否有效,因为我仍然需要在注册页面上提示account_name和account_type.

Mar*_*rry 23

最后使用嵌套属性使其工作.正如对肯顿答案的评论中所讨论的那样,这个例子是相反的.如果您希望每个帐户有多个用户,则必须先创建帐户,然后创建用户 - 即使您只创建一个用户也可以.然后,您可以绕过"设计"视图编写自己的"帐户"控制器和视图.如果您只是直接创建用户,那么发送确认电子邮件等的设计功能似乎仍然有效,即该功能必须是Devise 模型中自动化内容的一部分; 它不需要使用Devise控制器.

摘录相关文件:

模型中的应用程序/模型

class Account < ActiveRecord::Base
  has_many :users, :inverse_of => :account, :dependent => :destroy
  accepts_nested_attributes_for :users
  attr_accessible :name, :users_attributes
end

class User < ActiveRecord::Base
  belongs_to :account, :inverse_of => :users
  validates :account, :presence => true
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable
  attr_accessible :email, :password, :password_confirmation, :remember_me
end
Run Code Online (Sandbox Code Playgroud)

spec/models/account_spec.rb RSpec模型测试

it "should create account AND user through accepts_nested_attributes_for" do
  @AccountWithUser = { :name => "Test Account with User", 
                       :users_attributes => [ { :email => "user@example.com", 
                                                :password => "testpass", 
                                                :password_confirmation => "testpass" } ] }
  au = Account.create!(@AccountWithUser)
  au.id.should_not be_nil
  au.users[0].id.should_not be_nil
  au.users[0].account.should == au
  au.users[0].account_id.should == au.id
end
Run Code Online (Sandbox Code Playgroud)

配置/ routes.rb中

  resources :accounts, :only => [:index, :new, :create, :destroy]
Run Code Online (Sandbox Code Playgroud)

控制器/ accounts_controller.rb

class AccountsController < ApplicationController

  def new
    @account = Account.new
    @account.users.build # build a blank user or the child form won't display
  end

  def create
    @account = Account.new(params[:account])
    if @account.save
      flash[:success] = "Account created"
      redirect_to accounts_path
    else
      render 'new'
    end
  end

end
Run Code Online (Sandbox Code Playgroud)

views/accounts/new.html.erb视图

<h2>Create Account</h2>

<%= form_for(@account) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>

  <%= f.fields_for :users do |user_form| %>
    <div class="field"><%= user_form.label :email %><br />
    <%= user_form.email_field :email %></div>
    <div class="field"><%= user_form.label :password %><br />
    <%= user_form.password_field :password %></div>
    <div class="field"><%= user_form.label :password_confirmation %><br />
    <%= user_form.password_field :password_confirmation %></div>
  <% end %>

  <div class="actions">
    <%= f.submit "Create account" %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

Rails在复数与单数方面相当挑剔.既然我们说Account has_many用户:

  • 它期望模型和测试中的users_attributes(而不是user_attributes)
  • 它期望测试哈希数组,即使数组中只有一个元素,因此围绕{user attributes}的[].
  • 它期望控制器中的@ account.users.build.我无法让f.object.build_users语法直接在视图中工作.