This problem has been killing me. I played around with Ryan Bates complex forms, but I can't quite figure out my problem. I have this schema:
这个问题一直在扼杀我。我和Ryan Bates的复杂形式一起玩,但我无法弄清楚我的问题。我有这个架构:
Location has_many :targets
Target has_many :target_classifications
All locations are shown on the page. A user may create a target for any location dynamically through jscript, which then adds a table row under the location 3 selects (that contain available classifications to the target) and a target value. Any number of targets can be created for any location before clicking save.
I'm using rjs to render a target_partial, which has this code: I'm using fields_for in this way:
所有位置都显示在页面上。用户可以通过jscript动态地为任何位置创建目标,然后jscript在位置3选择(包含对目标的可用分类)和目标值下添加表行。在单击保存之前,可以为任何位置创建任意数量的目标。我正在使用rjs来渲染一个target_partial,它有以下代码:我正在以这种方式使用fields_for:
for each select. When sumbmitted, I get this hash:
每个选择。当sumbmitted时,我得到这个哈希:
"new_targets"=> {"7"=>[{"id"=>"13"}, {"id"=>"15"}, {"value"=>"67", "id"=>""}], "4"=> [{"id"=>"12"}, {"id"=>"15"}, {"value"=>"23", "id"=>""}, {"id"=>"11"}, {"id"=>"16"}, {"value"=>"67", "id"=>""}]},
So, it separates each target by location ("7" and "4" in this case), but doesn't separate each target. What I want is this:
因此,它按位置(在这种情况下为“7”和“4”)分隔每个目标,但不分隔每个目标。我想要的是这个:
"new_targets"=> {"7"=>[ {"target"=>[{"id"=>"13"}, {"id"=>"15"}, {"tonnes"=>"67"}]} ], "4"=>[ {"target"=>[{"id"=>"12"},{"id"=>"15"},{"tonnes"=>"23"]}, {"target"=>[{"id"=>"11"},{"id"=>"16"},{"tonnes"=>"67"]} ] }
so I can iterate through each target for each location. I can't seem to add in a new [target] brace in my field_for method (it blows up), but that's kind of what I want to do. Any thoughts?
所以我可以遍历每个位置的每个目标。我似乎无法在我的field_for方法中添加一个新的[target]大括号(它爆炸了),但这就是我想做的事情。有什么想法吗?
3 个解决方案
#1
I don't know if you are doing this, but you need to specify a string instead of the object when using fields_for. I can't see the codes you have in your partial, so I may be way off. Anyway, the way I do it is in a helper:
我不知道你是否这样做,但是在使用fields_for时你需要指定一个字符串而不是对象。我看不到你的部分代码,所以我可能会离开。无论如何,我这样做的方式是帮助:
def fields_for_target(target, &block)
prefix = target.new_record? ? 'new' : 'existing'
fields_for("location[#{prefix}_target_attributes][]", target, &block)
end
#2
This sort of issue is discussed here:
这里讨论这类问题:
http://wonderfullyflawed.com/2009/02/17/rails-forms-microformat/
But it seems to be a rails 2.3 solution (not an option for me). Basically, I want a form like they've posted on the site:
但它似乎是一个rails 2.3解决方案(对我来说不是一个选项)。基本上,我想要一个他们在网站上发布的表单:
<input name="creator[widget_attributes][0][id]" />
<input name="creator[widget_attributes][0][name]" />
<input name="creator[widget_attributes][0][price]" />
<input name="creator[widget_attributes][1][id]" />
<input name="creator[widget_attributes][1][name]" />
<input name="creator[widget_attributes][1][price]" />
Just some way to create a unique identifier for each target (or in this case, widget attribute) I want to add. Kind of tough given that a new target is added with jscript. I feel like there should be some way for rails to automatically do this for me
只是为我想要添加的每个目标(或者在本例中为widget属性)创建唯一标识符的某种方式。鉴于jscript添加了新目标,这种做法很难实现。我觉得应该有一些方法让rails自动为我做这件事
#3
Easy. Look up accepts_nested_attributes_for
. :)
简单。查找accepts_nested_attributes_for。 :)
#1
I don't know if you are doing this, but you need to specify a string instead of the object when using fields_for. I can't see the codes you have in your partial, so I may be way off. Anyway, the way I do it is in a helper:
我不知道你是否这样做,但是在使用fields_for时你需要指定一个字符串而不是对象。我看不到你的部分代码,所以我可能会离开。无论如何,我这样做的方式是帮助:
def fields_for_target(target, &block)
prefix = target.new_record? ? 'new' : 'existing'
fields_for("location[#{prefix}_target_attributes][]", target, &block)
end
#2
This sort of issue is discussed here:
这里讨论这类问题:
http://wonderfullyflawed.com/2009/02/17/rails-forms-microformat/
But it seems to be a rails 2.3 solution (not an option for me). Basically, I want a form like they've posted on the site:
但它似乎是一个rails 2.3解决方案(对我来说不是一个选项)。基本上,我想要一个他们在网站上发布的表单:
<input name="creator[widget_attributes][0][id]" />
<input name="creator[widget_attributes][0][name]" />
<input name="creator[widget_attributes][0][price]" />
<input name="creator[widget_attributes][1][id]" />
<input name="creator[widget_attributes][1][name]" />
<input name="creator[widget_attributes][1][price]" />
Just some way to create a unique identifier for each target (or in this case, widget attribute) I want to add. Kind of tough given that a new target is added with jscript. I feel like there should be some way for rails to automatically do this for me
只是为我想要添加的每个目标(或者在本例中为widget属性)创建唯一标识符的某种方式。鉴于jscript添加了新目标,这种做法很难实现。我觉得应该有一些方法让rails自动为我做这件事
#3
Easy. Look up accepts_nested_attributes_for
. :)
简单。查找accepts_nested_attributes_for。 :)