Clueless关于Rails ...请揭开如何在控制器视图和模型中使用变量的神秘面纱(Clueless About Rails… Demystify how to use variables across controllers views and models please)
我是ruby的新手,也是铁杆中的总菜鸟。 在阅读了Michael Hartl的教程之后,我的脑袋正在旋转着octothorps和黄瓜以及BDD。
我有使用Archaic Java和丑陋的jsp / servlet进行服务器端开发的经验。 我想要冷静学习铁轨。
需要帮助理解基本的Rails概念,即使在阅读本书之后,它也不会单击:
- 我以为我知道如何在模型,视图和控制器中使用darn实例变量,但是当我使用它们时,事情就不起作用了。 我需要规则,我可以在何处以及如何使用它们。 我有点如何使用从控制器到视图的实例变量。 但模型控制器? - 无能为力。
- 在模型中的方法,为什么它们需要是方法? 我可以创建变量并在我的控制器和视图中使用它们吗? 这部分是如何工作的。
- 嗯,有点像第一个问题..模型和控制器中的变量用法...... ??
I am new to ruby and a total noob in rails. My head is spinning with octothorps and cucumber and BDD after reading Michael Hartl's tutorial.
I have experience in server side development using Archaic Java and ugly jsp/servlets. I wanna be cool and learn rails.
Need help understanding basic Rails Concepts, even after reading the book, it just doesn't click:
- I thought I knew how to use the darn instance variables across the Models, views and controllers, but when I use them things don't work. I need the rules for where I can use them, when and how. I kinda get how to use instance variables from the controller to the view. But Model to controller? - Clueless.
- Methods in the model, why do they need to be methods? Can i just create variables and use them in my controllers and views? How does that part work.
- um, sort of like the first question.. Variable usage in models and controllers ... ??
原文:https://stackoverflow.com/questions/12851919
最满意答案
var max = 0; $('.post').attr("data-id", function(i,v){ max = +v > max ? +v : max; }); console.log( max ); // 5
其他方式:
var ids = $('.post').map(function(){ return +this.dataset.id; // or use jQ: return +$(this).data('id'); }); console.log( Math.max.apply(Math, ids) ); // 5
http://api.jquery.com/jquery.map/用于返回所需值的新数组。
我怎样才能找到JavaScript数组中包含的最大数字? 用于其余部分。一元
+
用于将任何可能的字符串数转换为Number
。
为了防止NaN
导致错误插入的Alpha字符导致的不匹配,您可以使用:return +this.dataset.id || 0; // Prevent NaN and turn "a" to 0
var max = 0; $('.post').attr("data-id", function(i,v){ max = +v > max ? +v : max; }); console.log( max ); // 5
Another way:
var ids = $('.post').map(function(){ return +this.dataset.id; // or use jQ: return +$(this).data('id'); }); console.log( Math.max.apply(Math, ids) ); // 5
http://api.jquery.com/jquery.map/ used to return a new Array of the desired values.
How might I find the largest number contained in a JavaScript array? is used for the rest.The unary
+
is used to convert any possible String number toNumber
.
To prevent aNaN
resulting a mismatch caused by a mistakenly inserted Alpha character you can use:return +this.dataset.id || 0; // Prevent NaN and turn "a" to 0
相关问答
更多-
最好的方法是使用数组并实现排序方法; 但是,这个例子对你来说更容易理解。 我们只是将每个数字与用户输入的当前最大数字进行比较: import java.util.Scanner; public class Blahh { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int largest = 0; int a = scan.n ...
-
试试这个查询我的朋友: select * from newsfeed where id in (select max(id) from (select id, case when update = 0 then id else update end update1 from newsfeed)t1 group by update1); SQL小提琴 Try this query my friend: select * from newsfeed where id in (select max(id) fr ...
-
使用Array.sort : var $wrapper = $('.testWrapper'); $wrapper.find('.test').sort(function(a, b) { return +a.dataset.percentage - +b.dataset.percentage; }) .appendTo($wrapper); 这是小提琴: http : //jsfiddle.net/UdvDD/ 如果您使用IE <10,则无法使用dataset属性。 改用getAttribute ...
-
不可以.Javascript使用浮点数来表示大值,值10 20超过浮点数的精度 。 在结果中添加相对较小的值(如20000或10000)时,生成的总和无法区分。 No. Javascript uses floating point numbers to represent large values, and the value 1020 exceeds the precision of the floating point numbers. When you add a relatively tiny va ...
-
这非常接近于数组中最大值的返回索引 。 但是如果你想获得所有索引,你可以只迭代数组,当前项目等于max时,将当前索引推送到argmax数组,这是当前发现的最大数量; 或者当您找到更多项目时更新max和argmax 。 var max = -Infinity, argmax = []; for(var i=0; i
max) max = array[i], argmax = [i]; else if(array[i] === max) ... -
([^>]*)是挑选额外的东西。 你应该能够通过/<\/?span>/g 。 或者,你也可以这样处理它: str = str.replace(/([^<]+)<\/span>/g, "$1") ([^>]*) is what's picking up the extras. You should be able to get by with just /<\/?span>/g. Alternatively, you could also handle it this way: str = str ...
-
这里的逻辑是错误的: (n1 % d !=0 && n2 % d != 0) 改成: (n1 % d !=0 || n2 % d != 0) 或者一旦看到n1 或 n2的除数,代码将停止,而不是它们的GCD,因为循环终止条件应该是你想要做的事情的否定。 The logic in here is wrong: (n1 % d !=0 && n2 % d != 0) change to: (n1 % d !=0 || n2 % d != 0) Or the code will stop once is ...
-
var max = 0; $('.post').attr("data-id", function(i,v){ max = +v > max ? +v : max; }); console.log( max ); // 5 其他方式: var ids = $('.post').map(function(){ return +this.dataset.id; // or use jQ: return +$(this).data('id'); }); console.log ...
-
带有LEAST和GREATEST函数的JPA CriteriaQuery(JPA CriteriaQuery with LEAST and GREATEST functions)[2022-02-23]
事实证明,CriteriaBuilder确实支持将LEAST和GREATEST作为非聚合函数调用,并且可以使用CriteriaBuilder.function(..)进行访问,如下所示: Predicate greatestPred = cb.equal(pathA.get(TableA_.col2), cb.function("greatest", String.class, pathX.get(TableX_ ... -
您可以使用ls选项-v 。 从man ls : -v natural sort of (version) numbers within text 如果你将内循环更改为 for f in `ls -v` ; do # iterate over files in sub-directory term="$term $f" done ls的结果将按数字顺序排序。 另一种选择是sort ,从man sort : -g, --general-numeric-sort compare accordi ...