您可以在用户模型中使用accepts_nested_attributes_for
(documentation)
它应该看起来像:
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
在视图中可以使用Rails的助手或手动创建领域:
<input type="text" name="user[organization_attributes][name]">
<% user = User.new(organization => Organization.new) %>
<%= form_for user do |form| %>
<%= form.fields_for user.organization do |organization_form| %>
<%= organization_form.text_field :name %>
<% end %>
<% end %>
编辑: 您的设计视图应该看起来像:
<h2>Sign up</h2>
<% resource.organization ||= Organization.new %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<%= f.fields_for resource.organization do |organization_form| %>
<div><%= organization_form.label :name %><br />
<%= organization_form.text_field :name %></div>
<% end %>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
的意见没有从外太空但是从色器件宝石来 - 你必须运行发电机,以创建编辑我认为的意见,但如果他们不创造了创业板的意见来代替。至于你的主要问题,你说你使用fields_for - 用户有accept_nested_attributes_for? – chrispanda
Thanx!现在我知道视图来自哪里。 – Lumbee