0

考虑以下:

客户.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

客户控制器.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

新的.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 等,就好像遍历数组一样。

我究竟做错了什么?

4

1 回答 1

1

你错过了

f.fields_for

只需添加它,它应该可以工作

于 2013-05-15T00:13:12.687 回答