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

我正在使用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  :destroy attr_accessible :name, :account_type end 

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

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

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

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

摘录相关文件:

app / models中的模型

 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 

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 

配置/ routes.rb中

  resources :accounts, :only => [:index, :new, :create, :destroy] 

控制器/ 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 

views / accounts / new.html.erb视图

 

Create Account

<%= form_for(@account) do |f| %> <%= render 'shared/error_messages', :object => f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :users do |user_form| %>
<%= user_form.label :email %>
<%= user_form.email_field :email %>
<%= user_form.label :password %>
<%= user_form.password_field :password %>
<%= user_form.label :password_confirmation %>
<%= user_form.password_field :password_confirmation %>
<% end %>
<%= f.submit "Create account" %>
<% end %>

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

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

难道你不能使用这些RailsCasts所涵盖的内容吗?:

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

您可以使用accepts_nested_attributes_for按照那些截屏video中的描述设置模型。

然后,您的views / devise / registrations / new.html.erb表单将适用于:用户正常,并且可以包含:account的嵌套表单。

在这种默认forms中是这样的:

 <%= f.fields_for :account do |account_form| %> 

<%= account_form.label :name, "Account Name", :class => "label" %> <%= account_form.text_field :name, :class => "text_field" %> (eg, enter your account name here)

<%= account_form.label :company, "Company Name", :class => "label" %> <%= account_form.text_field :company, :class => "text_field" %>

<% end %>

这是我正在处理的应用程序的示例代码,我正在使用simple_form gem,因此您的应用程序中使用的帮助程序可能会有所不同,但您可能会得到这个想法。

因此,当创建用户(他们注册时)时,他们还可以填写帐户模型将用于创建帐户的信息,一旦他们点击“注册”按钮。

并且您可能想要在该用户上设置属性,例如“admin”…听起来这个初始用户将是公司的“管理员”用户,尽管其他用户也可以访问。

希望有所帮助。

最好的解决方案是使用gem。

简单的方法: 粟米gem

子域方式: 公寓gem