Cuối cùng, hãy làm việc này bằng cách sử dụng các thuộc tính lồng nhau. Như đã thảo luận trong các ý kiến về câu trả lời của Kenton, ví dụ đó được đảo ngược. Nếu bạn muốn nhiều người dùng cho mỗi tài khoản, trước tiên bạn phải tạo Tài khoản, sau đó là Người dùng - ngay cả khi bạn chỉ tạo một người dùng để bắt đầu. Sau đó, bạn viết trình điều khiển và chế độ xem Tài khoản của riêng mình, bỏ qua chế độ xem Devise. Chức năng Devise để gửi email xác nhận, v.v. dường như vẫn hoạt động nếu bạn chỉ tạo trực tiếp người dùng, tức làchức năng đó phải là một phần của công cụ tự động hóa trong mô hình Devise; nó không yêu cầu sử dụng bộ điều khiển Devise.
Trích từ các tập tin liên quan:
Models trong ứng dụng/mô hình
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
đặc tả/mô hình/account_spec.rb RSpec mô hình thử nghiệm
it "should create account AND user through accepts_nested_attributes_for" do
@AccountWithUser = { :name => "Test Account with User",
:users_attributes => [ { :email => "[email protected]",
: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
config/routes.rb
resources :accounts, :only => [:index, :new, :create, :destroy]
controllers/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/tài khoản/new.html.erb xem
<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 là khá kén chọn về số nhiều so với số ít. Kể từ khi chúng ta nói Người dùng has_many tài khoản:
- dự kiến users_attributes (không user_attributes) trong mô hình và thử nghiệm
- nó hy vọng một mảng của băm cho kỳ thi này, ngay cả khi chỉ có một phần tử trong mảng , do đó [] xung quanh {thuộc tính người dùng}.
- dự kiến @ account.users.build trong bộ điều khiển. Tôi đã không thể nhận được cú pháp f.object.build_users để làm việc trực tiếp trong khung nhìn.
là chế độ xem "new.html.erb" (ví dụ: app/views/devise/registrations/new.html.erb)? – y0mbo
@ y0mbo, no, new.html.erb đang ở chế độ xem/tài khoản. Tôi đã thêm đường dẫn vào câu trả lời để làm rõ. Xem thêm đoạn đầu tiên về "bỏ qua chế độ xem Devise" và cách hoạt động. Về cơ bản tôi đang sử dụng tài khoản # tạo hành động để tạo cả tài khoản và người dùng, vì vậy tôi không cần chế độ xem Devise. –
Cảm ơn! Đây là một câu trả lời tuyệt vời và giải pháp rõ ràng. – y0mbo