具有Mongoid嵌入关系的表格 [英] Formtastic with Mongoid embedded_in relations
问题描述
有什么快速的方法可以为embeds_many-embedded_in关系建立表格? 我有以下内容:
Is there any quick way to make a form for embeds_many-embedded_in relation? I have the following:
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 ,但此处有待办事项".
I want to create a form for team with embedded editing for players. Seen https://github.com/bowsersenior/formtastic_with_mongoid_tutorial but "TODO" there.
推荐答案
我写了formtastic_with_mongoid_tutorial,但不幸的是,我还没有找到一种处理嵌入式关系的简单方法.我现在正在做的是在控制器中构建嵌入式对象,然后将这些对象传递到一个块中.看起来像这样:
I wrote the formtastic_with_mongoid_tutorial and unfortunately I haven't figured out an easy way of dealing with embedded relations yet. What I'm doing now is building the embedded objects in the controller, and then passing the objects into a block. It would look kind of like this:
= semantic_form_for @team do |form|
= @team.players.each do |player|
= form.inputs :for => [:players, player] do |player_form|
= player_form.input :name
别忘了处理Team
中的嵌套属性:
Don't forget to deal with nested attributes in 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
这绝对不是理想的选择.希望我能提供更多帮助,但这也许会为您指明正确的方向.如果有的话,我会随时留意更好的解决方案并更新本教程.
It is definitely far from ideal. Wish I could be of more help, but maybe this will point you in the right direction. I'll keep an eye out for better solutions and update the tutorial if/when I find any.
这篇关于具有Mongoid嵌入关系的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!