rails collection

编程入门 行业动态 更新时间:2024-10-11 23:18:42
本文介绍了rails collection_select 与 select的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

collection_select 和 select Rails 助手:我应该使用哪一个?

collection_select and select Rails helpers: Which one should I use?

我看不出这两种方式有什么不同.两个助手都获取一个集合并在选择标签内生成选项标签.是否存在 collection_select 比 select 更好的场景?或者我在这里缺少什么?

I can't see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a scenario where collection_select is better than select? or is anything I am missing here?

推荐答案

collection_select 旨在在项目列表是 ActiveRecord 对象数组时使用.collection_select 建立在 select 之上,因此当您需要显示对象集合而不是字符串数组时,这是一种方便的方法.

collection_select is intended to be used when the list of items is an array of ActiveRecord objects. collection_select is built on the top of select so it's a convenient method when you need to display a collection of objects and not an array of strings.

collection_select(:post, :author_id, Author.find(:all), :id, :name)

更多推荐

rails collection

本文发布于:2023-08-01 21:41:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:rails   collection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!