问题来了,
我实际上必须管理可以包含在 db 中定义的其他对象的对象。
例如,我有 5 种盒子。一个红框,一个绿框,一个蓝框,一个黄框,一个黑框。
每个盒子可以包含一个盒子,也可以包含一个盒子,依此类推。
我收到的是这种对象:
{
"id":1,
"type":"black",
"box":
{
"id":8,
"type":"red",
"box":
{
"id":15,
"type":"green",
"box":null
}
}
}
<select name="LEVEL_1">
<option value="0">NONE</option>
<option selected value="1">black</option>
<option value="8">red</option>
<option value="15">green</option>
<option value="3">blue</option>
<option value="10">yellow</option>
</select>
<br/>
<select name="LEVEL_2">
<option value="0">NONE</option>
<option selected value="8">red</option>
<option value="15">green</option>
<option value="3">blue</option>
</select>
<br/>
<select name="LEVEL_3">
<option value="0">NONE</option>
<option selected value="15">green</option>
<option value="10">yellow</option>
</select>
<br/>
<select name="LEVEL_4">
<option selected value="0">NONE</option>
<option value="15">green</option>
<option value="8">red</option>
<option value="3">blue</option>
<option value="10">yellow</option>
<option value="1">black</option>
</select><table>
<thead style="font-weight:bold;">
<tr style="background-color:lightblue;">
<td>Id</td>
<td>Type</td>
<td>Contains (sum)</td>
</tr>
</thead>
<tbody>
<tr ng-click="setCurrentBox();" style="background-color:lightgreen;">
<td>1</td>
<td>black</td>
<td>2 boxes</td>
</tr>
</tbody>
</table>ng-click部分。 setCurrentBox()函数在 Controller 中定义,它设置为 $scope.currentBox ,从“BoxService”接收的盒子对象。BoxService ,检索所选框的 json 对象(完全!将包含的框放入其中,如线程顶部所写),并将其分配给 $scope.currentBox多变的。$scope.currentBox . “currentBox”包含的框是属性。所以,不知何故,我认为我应该做一些 if object.box!=null然后阅读框...但是我对此有点迷茫...Box 0 (black one)
contains Box 1 (red one)
contains Box 2 (green one)
contains Box 3 (green one)
contains Box 4 (green one)
contains nothing (yet)
{
"id":"1",
"type":"black",
"box":{
"id":"8",
"type":"red",
"box":{
"id":"15",
"type":"green",
"box":{
"id":"15",
"type":"green",
"box":{
"id":"15",
"type":"green",
"box":null
}
}
}
}
}
Box 0 (all box colors choices available here!):
<!--This select contains all possible choices since it is the very first choice possible, no dependency-->
<select name="box0">
<option value="">NO CHOICE</option>
<option selected value="1">black</option>
<option value="8">red</option>
<option value="15">green</option>
<option value="3">blue</option>
<option value="10">yellow</option>
</select>
<br/>Box 1 (contained in box 0 box property) :
<!--This select contains only boxes choices that a black box can get (since it depends of box 0 value)-->
<select name="box1">
<option value="">NO CHOICE</option>
<option selected value="8">red</option>
<option value="15">green</option>
<option value="3">blue</option>
</select>
<br/>Box 2 (contained in box 1 box property) :
<!--This select contains only boxes choices that a red box can get (since it depends of box 1 value)-->
<select name="box2">
<option value="">NO CHOICE</option>
<option selected value="15">green</option>
<option value="10">yellow</option>
</select>
<br/>Box 3 (contained in box 2 box property) :
<!--This select contains only boxes choices that a green box can get (since it depends of box 2 value)-->
<select name="box3">
<option value="">NO CHOICE</option>
<option value="1">black</option>
<option value="8">red</option>
<option selected value="15">green</option>
<option value="3">blue</option>
<option value="10">yellow</option>
</select>
<br/>Box 4 (contained in box 3 box property) :
<!--This select contains only boxes choices that a green box can get (since it depends of box 3 value)-->
<select name="box4">
<option value="">NO CHOICE</option>
<option value="1">black</option>
<option value="8">red</option>
<option selected value="15">green</option>
<option value="3">blue</option>
<option value="10">yellow</option>
</select>
<br/>Box 5 (empty box ready to be filled in box 4 property) :
<!--This select contains only boxes choices that a green box can get (since it depends of box 4 value)-->
<!--This select has default selected value set as null since box4 box property is not set (box 4 box property is not a box, box 4 contains nothing)-->
<select name="box5">
<option value="" selected>NO CHOICE</option>
<option value="1">black</option>
<option value="8">red</option>
<option value="15">green</option>
<option value="3">blue</option>
<option value="10">yellow</option>
</select> {
"id":"1",
"type":"black",
"box":{
"id":"8",
"type":"red",
"box":{
"id":"15",
"type":"green",
"box":null
}
}
}
Box 0 (all box colors choices available here!):
<!--This select contains all possible choices since it is the very first choice possible, no dependency-->
<select name="box0">
<option value="">NO CHOICE</option>
<option selected value="1">black</option>
<option value="8">red</option>
<option value="15">green</option>
<option value="3">blue</option>
<option value="10">yellow</option>
</select>
<br/>Box 1 (contained in box 0 box property) :
<!--This select contains only boxes choices that a black box can get (since it depends of box 0 value)-->
<select name="box1">
<option value="">NO CHOICE</option>
<option selected value="8">red</option>
<option value="15">green</option>
<option value="3">blue</option>
</select>
<br/>Box 2 (contained in box 1 box property) :
<!--This select contains only boxes choices that a red box can get (since it depends of box 1 value)-->
<select name="box2">
<option selected value="">NO CHOICE</option>
<option value="15">green</option>
<option value="10">yellow</option>
</select> {
"id":"1",
"type":"black",
"box":{
"id":"3",
"type":"blue",
"box":null
}
}
Box 0 (all box colors choices available here!):
<!--This select contains all possible choices since it is the very first choice possible, no dependency-->
<select name="box0">
<option value="">NO CHOICE</option>
<option selected value="1">black</option>
<option value="8">red</option>
<option value="15">green</option>
<option value="3">blue</option>
<option value="10">yellow</option>
</select>
<br/>Box 1 (contained in box 0 box property) :
<!--This select contains only boxes choices that a black box can get (since it depends of box 0 value)-->
<select name="box1">
<option value="">NO CHOICE</option>
<option value="8">red</option>
<option value="15">green</option>
<option selected value="3">blue</option>
</select>
<br/>Box 2 (contained in box 1 box property) :
<!--This select contains only boxes choices that a blue box can get (since it depends of box 1 value)-->
<select name="box2">
<option selected value="">NO CHOICE</option>
<option value="15">green</option>
<option value="8">red</option>
<option value="3">blue</option>
<option value="10">yellow</option>
<option value="1">black</option>
</select> BoxService 中获得一个框的可能选择,或任何框的所有可能选择。 .这必须来自 BoxService .这个数据可能很大,在这个例子中很小,但这可能是一个长长的对象列表,可以包含在另一个对象中。最佳答案
试试这个例子:
http://jsfiddle.net/kevalbhatt18/0js7q638/1/
Using checkInnerObject function it will return count of 'box' see in example
function checkInnerObject(obj) {
var i = 0;
var arg = Array.prototype.slice.call(arguments, 1);
start: while (obj) {
if (obj.hasOwnProperty(arg)) {
obj = obj[arg];
i = i + 1;
continue start;
}
}
return i - 1;
}
checkInnerObject(OBJECT,'key you want to find');
关于javascript - 带有 AngularJS 的某种俄罗斯娃娃容器的动态编辑表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34045816/
使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做
有没有办法在Ruby中动态创建数组?例如,假设我想遍历用户输入的书籍数组:books=gets.chomp用户输入:"TheGreatGatsby,CrimeandPunishment,Dracula,Fahrenheit451,PrideandPrejudice,SenseandSensibility,Slaughterhouse-Five,TheAdventuresofHuckleberryFinn"我把它变成一个数组:books_array=books.split(",")现在,对于用户输入的每一本书,我想用Ruby创建一个数组。伪代码来做到这一点:x=0books_array.
假设我有一个类A,里面有一些方法。假设stringmethodName是这些方法之一,我已经知道我想给它什么参数。它们在散列中{'param1'=>value1,'param2'=>value2}所以我有:params={'param1'=>value1,'param2'=>value2}a=A.new()a.send(methodName,value1,value2)#callmethodnamewithbothparams我希望能够通过传递我的哈希以某种方式调用该方法。这可能吗? 最佳答案 确保methodName是一个符号,而
我想在IRB中浏览文件系统并让提示更改以反射(reflect)当前工作目录,但我不知道如何在每个命令后进行提示更新。最终,我想在日常工作中更多地使用IRB,让bash溜走。我在我的.irbrc中试过这个:require'fileutils'includeFileUtilsIRB.conf[:PROMPT][:CUSTOM]={:PROMPT_N=>"\e[1m:\e[m",:PROMPT_I=>"\e[1m#{pwd}>\e[m",:PROMPT_S=>"FOO",:PROMPT_C=>"\e[1m#{pwd}>\e[m",:RETURN=>""}IRB.conf[:PROMPT_MO
当我进入Rails控制台时,我已将pry设置为加载代替irb。我找不到该页面或不记得如何将其恢复为默认行为,因为它似乎干扰了我的Rubymine调试器。有什么建议吗? 最佳答案 我刚发现问题,pry-railsgem。忘记了它的目的是让“railsconsole”打开pry。 关于ruby-on-rails-带有Pry的Rails控制台,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我了解instance_eval和class_eval之间的基本区别。我在玩弄时发现的是一些涉及attr_accessor的奇怪东西。这是一个例子:A=Class.newA.class_eval{attr_accessor:x}a=A.newa.x="x"a.x=>"x"#...expectedA.instance_eval{attr_accessor:y}A.y="y"=>NoMethodError:undefinedmethod`y='forA:Classa.y="y"=>"y"#WHATTT?这是怎么回事:instance_eval没有访问我们的A类(对象)然后它实际上将它添加到
首先,我使用的是rails3.1.3和来自master的carrierwavegithub仓库的分支。我使用after_init钩子(Hook)来确定基于属性的字段页面模型实例并为这些字段定义属性访问器将值存储在序列化哈希中(希望它清楚我是什么谈论)。这是我正在做的事情的精简版:classPage省略mount_uploader命令让我可以访问我想要的属性。但是当我安装uploader时出现错误消息说“nil类的未定义新方法”我在源代码中读到有方法read_uploader和扩展模块中的write_uploader。我如何必须覆盖这些来制作mount_uploader命令使用我的“虚拟
我在一个简单的RailsAPI中有以下Controller代码:classApi::V1::AccountsControllerehead:not_foundendendend问题在于,生成的json具有以下格式:{id:2,name:'Simpleaccount',cash_flows:[{id:1,amount:34.3,description:'simpledescription'},{id:2,amount:1.12,description:'otherdescription'}]}我需要我生成的json是camelCase('cashFlows'而不是'cash_flows'
我正在尝试动态构建一个多维数组。我想要的基本上是这样的(为简单起见写出来):b=0test=[[]]test[b]这给了我错误:NoMethodError:undefinedmethod`test=[[],[],[]]而且它工作正常,但在我的实际使用中,我不会事先知道需要多少个数组。有一个更好的方法吗?谢谢 最佳答案 不需要像您正在使用的索引变量。只需将每个数组附加到您的test数组:irb>test=[]=>[]irb>test[["a","b","c"]]irb>test[["a","b","c"],["d","e","f"]]