将关联数据从数据库加载到 edit.html.erb

发布于 2024-08-24 20:13:08 字数 2039 浏览 13 评论 0原文

我有以下一对多关联。文档有许多节,节有许多项目。

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 %>

我必须指定带有值属性集的选项哈希,例如:

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 %> 

无效吗?非常感谢。

I have the following one to many associations. Document has many Sections and Section has many Items.

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

And the 'edit' action as follows :-

def edit
  @document = Document.find(params[:id])
end

Here is the 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 %>

I have to specify the options hash with the value attribute set, for eg:

options = {:value => item.comments} 

in order to show the item comments when I click the 'edit' link to modify the item comments. Shouldn't they be loaded by default, which seems to be the case for header comments.

Thanks for replying. Yes, i want to render the text area with the item.comments value from the database. The below code I had, does not load the comments.

<% s.fields_for :item, :index => item.id do |i| %>
 <p>
  <%= i.text_area :comments %> 
 </p>
<% end %>

can you explain me why

<%= text_area(:item, :comments) %> 

works but

<%= i.text_area :comments %> 

does not. Thanks much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-08-31 20:13:08

看来您对 options 的理解不正确。 这是它是什么:

输入标记上的其他选项可以作为带有选项的哈希传递

这意味着 options 设置 HTML 标记的属性。

您没有在问题中指定您到底想做什么,但我假设您想以 item.comments 作为值来渲染 textarea 标记。如果是这样,那么您可以使用第二个参数方法(请参阅docs)并尝试以下操作:

text_area(:item, :comments, :size => "20x30")
# => <textarea cols="20" rows="30" id="item_comments" name="item[comments]">
#      #{@item.comments}
#    </textarea>

It seems your understanding of options is not correct. Here is what it is:

Additional options on the input tag can be passed as a hash with options

Which means that options sets attributes for the HTML tag.

You did not specify what exactly you want to do in the question, but I assume you want to render textarea tag with item.comments as a value. If so then you can use the 2nd parameter method (see the docs) and try this:

text_area(:item, :comments, :size => "20x30")
# => <textarea cols="20" rows="30" id="item_comments" name="item[comments]">
#      #{@item.comments}
#    </textarea>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文