请考虑以下事项:
customer.rb
module Refinery
class Customer < Refinery::Core::BaseModel
has_many :users, :class_name => "Refinery::User"
accepts_nested_attributes_for :users
end
end
end
user_decorator.rb
Refinery::User.class_eval do
belongs_to :customer, :class_name => 'Refinery::Customer'
end
customer_controller.rb
module Refinery
class UsersController
def new
@customer = ::Refinery::Customer.new
# tried using build here as well with no sucess
@owner = @customer.users.new
@inputer = @customer.users.new
# raise @customer.users.length.to_yaml => returns 2 so that works!
end
end
end
new.html.erb
<%= form_for @customer do |f| %>
<% if @customer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@customer.errors.count, "error") %> need to be corrected before continuing:</h2>
<ul>
<% @customer.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<!-- company fields here -->
<%= fields_for :users do |user| %>
<div>
<%= user.label :first_name %>
<%= user.text_field :first_name %>
</div>
<div>
<%= user.label :last_name %>
<%= user.text_field :last_name %>
</div>
<!-- more user fields here etc -->
<% end $>
<% end %>
当我查看此页面时,只显示一个用户。字段的ID也是这样的:
<div>
<label for="users_last_name">Last name</label>
<input id="users_last_name" name="users[last_name]" size="30" type="text">
</div>
我认为那里应该有某种索引吗? (即0,1,2等,就好像在数组上迭代一样。
我做错了什么?
答案 0 :(得分:1)
你错过了
f.fields_for
只需添加它就可以了