将额外结果添加到Association Collection Proxy结果中

时间:2015-07-08 11:44:18

标签: ruby-on-rails ruby-on-rails-4 formtastic

我有两个型号,

formtastic gem

我使用edit,请users_controller user行动。表单的所有必需posts和关联的<%= semantic_form_for @user do |f| %> <%= f.input :name %> <%= f.inputs :posts do |p| %> <%= p.input :title %> <%= p.input :comment %> <% end %> <% end %> 属性将以formtastic形式预先填写

代码:

@user

例如,我有一个posts和两个@user.posts相关联。 在做 [ [0] #<Post:0x0000000aa53a20> { :id => 3, :title => 'Hello World', :comment => 'Long text comes here' }, [1] #<Post:0x0000000aa53a41> { :id => 5, :title => 'Hello World 2', :comment => 'Long text comes here too' } ] 时,结果就像。

post

因此表单将包含两个要编辑的帖子字段。

实际上,我想在这两个帖子之前再张一张空白帖子。

通过在{0}位置向@object.posts结果插入新的空@object.posts个对象,可以轻松实现此目的。

所以,我想要的 [ [0] #<Post:0x0000000aa53a50> { :id => nil, :title => nil, :comment => nil }, [1] #<Post:0x0000000aa53a20> { :id => 3, :title => 'Hello World', :comment => 'Long text comes here' }, [2] #<Post:0x0000000aa53a41> { :id => 5, :title => 'Hello World 2', :comment => 'Long text comes here too' } ] 结果应该看起来像,

@user.posts

从{{1}}获得此结构的任何解决方案?

1 个答案:

答案 0 :(得分:2)

#edit操作中执行以下操作:

def edit
  #... your code
  @user.posts << Post.new
end
相关问题