Code
  • 首页
  • cpu-architecture
  • performance
  • c++
  • branch-prediction
  • java
2025-03-18 08:00:10

Rails获取“each”循环的索引

rubyruby-on-rails

我有这样一个循环:

<% @images.each do |page| %>

<% end %>

我如何得到循环内的“页”的索引?


<% @images.each_with_index do |page, index| %>

<% end %>
2011-01-27 00:12:07

each_with_index试试。

2011-01-27 00:13:44

这两个答案都很好。 我还建议你一个类似的方法:

<% @images.each.with_index do |page, index| %>
<% end %>

你可能看不出这和公认答案之间的区别。让我把你的眼睛引向这些方法调用:.each。With_index是。each和。With_index。

2011-01-27 00:16:23

推荐文章

  • 无法安装gem -未能建立gem本地扩展-无法加载这样的文件——mkmf (LoadError)
  • 如何在Ruby中创建文件
  • 什么是Ruby文件。开放模式和选项?
  • Ruby数组到字符串的转换
  • 如何分割(块)一个Ruby数组成X元素的部分?
  • Ruby中“or”和||的区别?
  • 在Rails中使用user_id:integer vs user:references生成模型
  • 如何测试参数是否存在在轨道
  • 验证多个列的唯一性
  • Rails:在where语句中使用大于/小于
  • Rails:如何为Rails activerecord的模型中的属性创建默认值?
  • 库未加载:libmysqlclient.16。在OS X 10.6上使用mysql2 gem运行'rails server'时出现dylib错误
  • 在Ruby中不创建新字符串而修饰字符串的规范方法是什么?
  • RVM不是一个函数,用' RVM use…’不会起作用
  • 了解Gemfile。锁文件

aliyun

最新文章

  • 如何在iOS中使用Swift编程segue
  • $destroy是否删除事件监听器?
  • Swift -整数转换为小时/分钟/秒
  • 如何在Windows命令提示符下运行.sh ?
  • Android API 21工具栏填充
  • 如何舍入一个双到最近的Int在迅速?
  • Nginx提供下载。php文件,而不是执行它们
  • 在网页上用鼠标模拟震颤(例如帕金森病)?
  • Android L中不支持操作栏导航模式
  • 扁平化数组的数组在Swift
  • Swift:声明一个空字典
  • 即使模板文件存在,Flask也会引发TemplateNotFound错误
  • Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
  • “你有邮件”的消息在终端,os X
  • console.log()和console.debug()的区别?

标签

cpu-architectureperformancec++branch-predictionjavaundogit-commitgitversion-controlgit-remotegit-pushgit-branchgit-pullgit-fetchpythongeneratoriteratorjsonmime-typescontent-typearraysjavascriptgit-addcode-formattingcstandards-complianceoperatorscommentsdynamic-memory-allocationstackmemory-managementheap-memorylanguage-agnosticoverwritebackground-colorbrowserhtmldomjqueryvisibilityuse-strictsyntaxjslintremote-branchgit-checkoutpython-moduleidiomsprogram-entry-pointnamespacesbranch
2025 code 京ICP备15047053号-1