rails collection_select 与 select [英] rails collection_select vs. select

查看:60
本文介绍了rails collection_select 与 select的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

collection_selectselect Rails 助手:我应该使用哪一个?

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

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

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_select 与 select的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆