首页 \ 问答 \ Clueless关于Rails ...请揭开如何在控制器视图和模型中使用变量的神秘面纱(Clueless About Rails… Demystify how to use variables across controllers views and models please)

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概念,即使在阅读本书之后,它也不会单击:

  1. 我以为我知道如何在模型,视图和控制器中使用darn实例变量,但是当我使用它们时,事情就不起作用了。 我需要规则,我可以在何处以及如何使用它们。 我有点如何使用从控制器到视图的实例变量。 但模型控制器? - 无能为力。
  2. 在模型中的方法,为什么它们需要是方法? 我可以创建变量并在我的控制器和视图中使用它们吗? 这部分是如何工作的。
  3. 嗯,有点像第一个问题..模型和控制器中的变量用法...... ??

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:

  1. 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.
  2. 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.
  3. um, sort of like the first question.. Variable usage in models and controllers ... ??

原文:https://stackoverflow.com/questions/12851919
更新时间:2022-03-26 11:03

最满意答案

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 to Number.
To prevent a NaN 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

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)