我遇到了awesome_nested_set在select中复制子项的问题(如标题所示)
我在表格中有以下数据(phpmyadmin在字段周围加上引号):
id name parent_id lft rgt created_at updated_at
"12" "BNW" NULL "1" "8" "2010-12-01 22:23:36" "2010-12-01 22:23:36"
"13" "NYSSA" "12" "2" "3" "2010-12-01 22:23:48" "2010-12-01 22:25:12"
"14" "BOARDMAN" "12" "4" "5" "2010-12-01 22:25:28" "2010-12-01 22:25:28"
"15" "QUINCY" "12" "6" "7" "2010-12-01 22:25:37" "2010-12-01 22:25:37"
"16" "WCC" NULL "9" "16" "2010-12-02 18:08:00" "2010-12-02 18:08:00"
"17" "CALIFORNIA" "16" "10" "11" "2010-12-02 18:08:08" "2010-12-02 18:08:08"
"18" "NORTH POWDER" "16" "12" "13" "2010-12-02 18:08:16" "2010-12-02 18:08:16"
"19" "ELLENSBURG" "16" "14" "15" "2010-12-02 18:08:25" "2010-12-02 18:08:25"
型号:
class DestinationGroup < ActiveRecord::Base
acts_as_audited
has_one :purchase
acts_as_nested_set
end
控制器:
class PurchasesController < ApplicationController
def new
@destination_groups = DestinationGroup.all
...
end
...
查看:
= f.select :destination_group_id, options_for_select(nested_set_options(@destination_groups) {|i| "#{'-' * i.level} #{i.name}" } )
如果您需要更多信息我可以发布,但我认为这与问题真正相关。
修改
我得到这个HTML:
<select id="purchase_destination_group_id" name="purchase[destination_group_id]">
<option value="12"> BNW</option>
<option value="13">- NYSSA</option>
<option value="14">- BOARDMAN</option>
<option value="15">- QUINCY</option>
<option value="13">- NYSSA</option>
<option value="14">- BOARDMAN</option>
<option value="15">- QUINCY</option>
<option value="16"> WCC</option>
<option value="17">- CALIFORNIA</option>
<option value="18">- NORTH POWDER</option>
<option value="19">- ELLENSBURG</option>
<option value="17">- CALIFORNIA</option>
<option value="18">- NORTH POWDER</option>
<option value="19">- ELLENSBURG</option>
</select>
修改2
这更令人讨厌:
DestinationGroup.all.collect{|i| "#{'-' * i.level} #{i.name}" }
=> [" BNW", "- NYSSA", "- BOARDMAN", "- QUINCY", " WCC", "- CALIFORNIA", "- NORTH POWDER", "- ELLENSBURG"]
这显然很好。
我在视图中这样做了:
= "<!-- #{@destination_groups.inspect} -->"
<!--
[
#<DestinationGroup id: 12, name: "BNW", parent_id: nil, lft: 1, rgt: 8, created_at: "2010-12-01 22:23:36", updated_at: "2010-12-01 22:23:36">,
#<DestinationGroup id: 13, name: "NYSSA", parent_id: 12, lft: 2, rgt: 3, created_at: "2010-12-01 22:23:48", updated_at: "2010-12-01 22:25:12">,
#<DestinationGroup id: 14, name: "BOARDMAN", parent_id: 12, lft: 4, rgt: 5, created_at: "2010-12-01 22:25:28", updated_at: "2010-12-01 22:25:28">,
#<DestinationGroup id: 15, name: "QUINCY", parent_id: 12, lft: 6, rgt: 7, created_at: "2010-12-01 22:25:37", updated_at: "2010-12-01 22:25:37">,
#<DestinationGroup id: 16, name: "WCC", parent_id: nil, lft: 9, rgt: 16, created_at: "2010-12-02 18:08:00", updated_at: "2010-12-02 18:08:00">,
#<DestinationGroup id: 17, name: "CALIFORNIA", parent_id: 16, lft: 10, rgt: 11, created_at: "2010-12-02 18:08:08", updated_at: "2010-12-02 18:08:08">,
#<DestinationGroup id: 18, name: "NORTH POWDER", parent_id: 16, lft: 12, rgt: 13, created_at: "2010-12-02 18:08:16", updated_at: "2010-12-02 18:08:16">,
#<DestinationGroup id: 19, name: "ELLENSBURG", parent_id: 16, lft: 14, rgt: 15, created_at: "2010-12-02 18:08:25", updated_at: "2010-12-02 18:08:25">
]
-->
谢谢
答案 0 :(得分:0)
我想通了,我正在使用:
# controller
@destination_groups = DestinationGroup.all
然后:
# view
= f.select :destination_group_id, options_for_select(nested_set_options(@destination_groups) {|i| "#{'-' * i.level} #{i.name}" } )
我把它改为:
# view
= f.select :destination_group_id, options_for_select(nested_set_options(DestinationGroup) {|i| "#{'-' * i.level} #{i.name}" } )
我猜它是从DestinationGroup.all重复它,无论如何它固定的任何人再次遇到它。