【问题标题】:Keep Child Objects sorted in a field_for保持子对象在 field_for 中排序
【发布时间】:2013-04-09 07:31:09
【问题描述】:

我有这样的代码:

user.posts.size #=> 5 already saved
user.posts.new(title:"foo")
user.posts.new(title:"bar")
user.posts.sort_by! { |e| e.title } #=> sort correclty
user.posts #=> sorted but with the saved on the top, the new one at the bottom

我使用field_for 的排序,但显然它仍未排序。

使用:Rails 3.2.11 和 Mongoid 3.0.23

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongoid mongoid3


    【解决方案1】:

    我不确定为什么会这样,但我认为这会解决问题

    # controller
    user.posts.build(title:"foo")
    user.posts.build(title:"bar")
    
    @posts = user.posts.sort_by(&:title)
    
    # view
    = f.fields_for :posts, @posts do |post_form|
      ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 2016-11-14
      • 2010-10-04
      相关资源
      最近更新 更多