我已經搜索了大約一個小時,並且發現了大量描述如何將字段添加到Devise用戶模型的問題。但是,我找不到任何明確解釋如何將一個或多個模型添加到註冊過程的解釋。Rails使用其他模型設計註冊
在註冊時,我希望用戶填寫電子郵件地址,密碼以及我的客戶端模型,公司模型和地址模型(以便我可以正常運行Web應用程序所需的所有信息)。
我的模特都是這樣
user.rb
class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me, :client belongs_to :client end
client.rb
class Client < ActiveRecord::Base attr_accessible :bankaccount, :email, :logo, :mobile, :phone, :website has_many :users has_one :company has_one :address accepts_nested_attributes_for :company, :address end
我認爲要做到這一點的唯一方法是創建自己的RegistrationsController所以我可以做@client = Client.new
,然後做到這一點,我認爲:
<%= f.simple_fields_for @client do |ff| %> <%= f.simple_fields_for :company do |fff| %> <% field_set_tag t(:company) do %> <%= ff.input :name %> <% end %> <% end %> <%= f.simple_fields_for :address do |fff| %> //address inputs <% end %> <% end %> <fieldset> <legend><%= t(:other) %></legend> // other inputs </fieldset> <% end %>
的原因,我需要這樣的工作方式是因爲我有多個用戶可以代表同一個客戶端(因此需要訪問相同的數據)。我的客戶擁有應用程序中的所有數據,因此需要在應用程序可用之前創建。