我正在使用 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
问题是,当第一个用户注册时,我如何创建帐户和用户?
类似于答案 here ?我不知道怎么做
创建帐户,然后将其传递给设计以创建用户。
和 account_type 到 User 模型,并创建一个新的 Account
记录account_id是否传入?在这种情况下,我试图从 Devise 隐藏帐户创建,但不确定是否会起作用,因为我仍然需要在注册页面上提示 account_name 和 account_type。
最佳答案
最后使用嵌套属性让它工作。正如对肯顿答案的评论中所讨论的那样,这个例子是相反的。如果您希望每个帐户有多个用户,则必须先创建帐户,然后再创建用户——即使您一开始只创建一个用户。然后你编写自己的 Accounts Controller 和 View ,绕过 Devise View 。如果您直接创建用户,用于发送确认电子邮件等的设计功能似乎仍然有效,即该功能必须是设计模型中自动魔法的一部分;它不需要使用设计 Controller 。
相关文件的摘录:
型号 在应用程序/模型中
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
规范/模型/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
配置/路由.rb
resources :accounts, :only => [:index, :new, :create, :destroy]
Controller /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
意见/帐户/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 %>
Rails 对复数和单数非常挑剔。既然我们说 Account has_many Users:
关于ruby-on-rails - 将 Account 和 User 表与 Devise 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8305120/