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

推荐文章

  • 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
  • 是否可以在MiniTest中运行单个测试?
  • 如何在Ruby中生成a和b之间的随机数?
  • ActiveRecord OR查询
  • 无法安装gem -未能建立gem本地扩展-无法加载这样的文件——mkmf (LoadError)
  • 如何在Ruby中创建文件
  • 什么是Ruby文件。开放模式和选项?
  • Ruby数组到字符串的转换
  • 如何分割(块)一个Ruby数组成X元素的部分?
  • Ruby中“or”和||的区别?
  • 在Rails中使用user_id:integer vs user:references生成模型
  • 如何测试参数是否存在在轨道
  • 验证多个列的唯一性
  • Rails:在where语句中使用大于/小于
  • Rails:如何为Rails activerecord的模型中的属性创建默认值?

aliyun

最新文章

  • 如何删除默认的导航栏空间在SwiftUI导航视图
  • 我可以列出所有的标准Go包吗?
  • 证书验证失败:无法获得本地颁发者证书
  • 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
  • 打开同一目录两次
  • 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
  • NullInjectorError: AngularFirestore没有提供程序
  • 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
  • 从元组/数组值派生联合类型
  • 如何在Angular2中截断文本?
  • 安装tzdata非交互式
  • ionic 2 - Error在Android Studio中找不到Gradle的安装版本
  • 如何查看所有地区所有正在运行的Amazon EC2实例?
  • 当使用pip3安装包时,“Python中的ssl模块不可用”
  • 安装APK时出现错误

标签

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