0

以前我能够呈现特定关联的多个实例(生成属于团队的多个角色 [团队有许多角色 - 角色属于团队] 但是在对布局进行一些更改后它不再这样做。我已经通读许多相关的线程,但似乎没有一个能解决我的问题。

我有一个多模型表单,我正在尝试构建多个角色关联并让它们在项目创建视图中多次呈现。我遇到的问题是它只会渲染一次。

#projects_controller.rb
def new
@user = current_user
@project = Project.new
@team = Team.new
@team = @project.build_team
#Default - New Project generates two roles through a team
2.times{@team.roles.new
        role = @team.roles.build}  
 end

#Models

  class Project < ActiveRecord::Base

    has_one :team, dependent: :destroy
    has_many :roles, :through => :team
     accepts_nested_attributes_for :team, :allow_destroy => :true
   end



 class Team < ActiveRecord::Base
      attr_accessible :name, :roles_attributes
      validates :project, presence: true
      belongs_to :project
      has_many :roles, dependent: :destroy

      accepts_nested_attributes_for :roles, :allow_destroy => :true
 end

class Role < ActiveRecord::Base
 belongs_to :team
end

#Views
#new.html.erb
 <%= form_for :project do |f| %>
      <%= render 'fields', :project_form => f %>
      <%= content_tag(:button, content_tag(:span, "Create Project"), {:class => "btn- cma-2 bolds", :type=>:submit}) %> 
     <% end %>

#_fields.html.erb 
  <%= project_form.fields_for :roles do |f| %>
        <%= render 'shared/role_fields', :role_form => f %>
   <% end %>

#_role_fields.html.erb
<div class="formAreaWhite clearfix">
  <div class="width350 fleft">
    <p class="blue">Title:</p>
    <div class="formArea"><%= role_form.collection_select(:role_title_id, @roletitles, :id, :title, {:prompt => "Select a Role"}, {:class=>"formSelect", :size => '1'}) %>
    </div>


    <p><span class="blue">Skills:</span>6/6</p>
  </div>

  <div class="width350 fright">
    <p class="blue">Duties:</p>
    <div class="formArea">

      <div class="formArea"><%= role_form.text_area :duty, :class=>"text300" %></div>
    </div>

</div>
4

1 回答 1

0

我意识到了这个问题。我在更新模板时删除了 fields_for :team 标记,因为我希望隐藏团队值但存在关联。显然,团队实例必须出现在视图中才能建立角色关联。

一旦将此标签添加回视图中,就会按预期生成角色的实例。

于 2013-02-02T23:05:18.133 回答