【问题标题】:Formtastic with Mongoid embedded_in relationsFormtastic 与 Mongoid 嵌入关系
【发布时间】:2010-12-22 07:40:07
【问题描述】:

有没有什么快速的方法可以为 embeds_many-embedded_in 关系制作表格? 我有以下内容:

class Team
  include Mongoid::Document
  field :name, :type => String
  embeds_many :players
end

class Player
  include Mongoid::Document
  embedded_in :team, :inverse_of => :players
  field :name, :type => String
end

我想为团队创建一个表格,并为玩家提供嵌入式编辑功能。看到https://github.com/bowsersenior/formtastic_with_mongoid_tutorial,但在那里看到“TODO”。

【问题讨论】:

    标签: ruby-on-rails mongoid formtastic


    【解决方案1】:

    我编写了formtastic_with_mongoid_tutorial,不幸的是我还没有找到一种简单的方法来处理嵌入式关系。我现在正在做的是在控制器中构建嵌入式对象,然后将对象传递到一个块中。它看起来像这样:

    = semantic_form_for @team do |form|
      = @team.players.each do |player|
        = form.inputs :for => [:players, player] do |player_form|
          = player_form.input :name
    

    别忘了处理Team中的嵌套属性:

    class Team
      include Mongoid::Document
      accepts_nested_attributes_for :players, 
        :allow_destroy => true, 
        # formtastic sends blank attributes to Mongoid models if you use checkboxes
        :reject_if => proc { |attributes| 
          attributes['name'].blank? && attributes['_destroy'].blank? 
        }
       # ...
    end
    

    这绝对远非理想。希望我能提供更多帮助,但也许这会为您指明正确的方向。我会留意更好的解决方案,如果/当我发现任何问题时更新教程。

    【讨论】:

      猜你喜欢
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多