我有以下一对多的关联。文档有很多部分,部分有很多项目。
class Document < ActiveRecord::Base
has_many :document_sections, :dependent => :destroy, :autosave => true
has_many :document_items, :through => :document_sections
end
class DocumentSection < ActiveRecord::Base
belongs_to :document
has_many :document_items, :dependent => :destroy, :autosave => true
end
class DocumentItem < ActiveRecord::Base
belongs_to :document_section
end
而“编辑”操作如下:-
def edit
@document = Document.find(params[:id])
end
这是edit.html.erb
<h1>Edit!</h1>
<% form_for(@document) do |f| %>
<%= f.error_messages %>
<p>
<p> Header Comment <p/><br />
<%= f.text_field :comment %>
<%= f.hidden_field :uid %>
</p>
<% @document.document_sections.each do |section| %>
<% f.fields_for :section, :index => section.id do |s| %>
<p>
<%= s.hidden_field :seqnum, options = {:value => section.seqnum} %>
</p>
<% section.document_items.each do |item| %>
<% s.fields_for :item, :index => item.id do |i| %>
<p>
<%= i.text_area :comments, options = {:value => item.comments} %>
</p>
<% end %>
<% end %>
<% end %>
<% end %>
<p>
<%= f.submit "Submit Comments" %>
</p>
<% end %>
我必须使用 value 属性集指定选项哈希,例如:
options = {:value => item.comments}
为了在我单击“编辑”链接修改项目评论时显示项目评论。默认情况下不应该加载它们,标题注释似乎就是这种情况。
感谢回复。是的,我想使用数据库中的 item.comments 值呈现文本区域。我的以下代码不加载评论。
<% s.fields_for :item, :index => item.id do |i| %>
<p>
<%= i.text_area :comments %>
</p>
<% end %>
你能解释一下为什么吗
<%= text_area(:item, :comments) %>
有效,但
<%= i.text_area :comments %>
才不是。非常感谢。