有一个@avalibable_colors=['#5A009D', '#004DFF', '#F4F400', '#FF8000']
变量(它不是模型的实例),它代表可用的选项 - 颜色,还没有被采用,通过方法:
def choose_color
@check=Check.find(params[:check_id])
@colorschemes=@check.colorschemes
current_colors=[]
@check.colorschemes.each do |c|
current_colors.push(c.color)
end
@avalibable_colors=['#5A009D', '#004DFF', '#F4F400', '#FF8000']
current_colors.each do |c|
if @avalibable_colors.index(c)
@avalibable_colors[@avalibable_colors.index(c)]=nil
end
end
@avalibable_colors.compact!
respond_to do |format|
format.js
end
end
之后的@avalibable_colors
内容只有未采取的颜色选项。
通常它可以工作。如果我选择 1 个颜色选项,它显示其余 4,如果 2 - 其余 3,如果我选择一个,然后删除它 - 它是可用的颜色。
这一直有效到整整四个。如果我添加所有四个,然后一个一个删除,在我刷新页面之前没有颜色可用。
服务器日志:
Started GET "/choose_color?check_id=46" for 127.0.0.1 at 2013-06-13 19:00:31 +0400
Processing by ColorschemesController#choose_color as JS
Parameters: {"check_id"=>"46"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."auth_token" = 'oPXJtztDkYdYmVsQ3wxxfQ' LIMIT 1
Check Load (1.0ms) SELECT "checks".* FROM "checks" WHERE "checks"."id" = 46 LIMIT 1
CACHE (0.0ms) SELECT "checks".* FROM "checks" WHERE "checks"."id" = 46 LIMIT 1
Check Load (1.0ms) SELECT "checks".* FROM "checks" WHERE "checks"."id" = $1 LIMIT 1 [["id", "46"]]
Colorscheme Load (0.0ms) SELECT "colorschemes".* FROM "colorschemes" WHERE "colorschemes"."check_id" = 46
Rendered colorschemes/_choose_color.html.erb (0.0ms)
Rendered colorschemes/choose_color.js.erb (5.0ms)
Completed 200 OK in 33ms (Views: 22.0ms | ActiveRecord: 3.0ms)