Ruby on rails 通过所属对象编辑对象

Ruby on rails 通过所属对象编辑对象,ruby-on-rails,relationship,has-many,belongs-to,Ruby On Rails,Relationship,Has Many,Belongs To,因此,当我编辑线程对象时,我试图只编辑第一个Post条目 以下是我的设置: class Thread < ActiveRecord::Base attr_accessible :name, :posts_attributes has_many :posts, dependent: :destroy accepts_nested_attributes_for :posts, reject_if: lambda { |a| a[:content].blank? } end 如果我按原样运行

因此,当我编辑线程对象时,我试图只编辑第一个Post条目

以下是我的设置:

class Thread < ActiveRecord::Base
attr_accessible :name, :posts_attributes

has_many :posts, dependent: :destroy
accepts_nested_attributes_for :posts, reject_if: lambda { |a| a[:content].blank? }
end
如果我按原样运行这段代码,那么我会得到一个页面,在那里我可以编辑所有的帖子,我想这是因为:帖子是从一个关联中提取的。但我只想要第一个帖子


然后,如果我将:posts更改为@discussion.posts.at0以获得第一篇帖子,那么我可以继续编辑页面。。。表单_正确显示,但是在触发更新时,它说我不能编辑受保护的属性:posts。。。关于如何只显示和编辑第一篇帖子有什么想法吗?

添加attr\u accessible:posts会有帮助吗?如果你尝试一下这个@random,会怎么样?很遗憾,它没有起作用:/woooooo!成功了!谢谢az7ar。
class Post < ActiveRecord::Base
attr_accessible :content, 

belongs_to :discussion
end
<%= provide(:title, "Edit Discussion")%>
<%= form_for(@thread) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>

    <%= f.label :name %>
    <%= f.text_field :name, size: 60 %>

    <%= f.fields_for :posts do |builder| %>

        <%= builder.label :content %>
        <%= builder.text_area :content, :size => "60x15" %>
    <% end %>

    <%= f.submit "Edit Thread" %>
<% end %>