以前,我能够呈现特定关联的多个实例(生成属于团队团队的多个角色具有-多个角色-角色属于团队,但在对布局进行了一些更改后,它不再这样做。我已经阅读了许多相关的帖子,但似乎没有一个能解决我的问题。
我有一个多模型表单,我正在尝试构建多个角色关联,并在项目创建视图中多次呈现它们。我遇到的问题是它只能渲染一次。
#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>发布于 2013-02-03 07:05:18
我意识到了问题所在。在对模板进行更新时,我删除了fields_for :team标记,因为我希望隐藏团队值,但关联仍然存在。显然,为了建立角色关联,团队实例必须出现在视图中。
将此标记添加回视图后,角色的实例将按预期生成。
https://stackoverflow.com/questions/14666499
复制相似问题