如何从rails中的给定列表创建选择框?

我从控制器传递一个列表

@distinct_grade_list = Student.uniq.pluck(:grade) 

从模型学生创建不同的成绩列表

现在在我的视图页面中,如何将其显示为我正在使用的选择框

  

现在我需要保留A,B,C,D

 collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) public 

为什么不读取api文档和样本 ?

样品用量:

 class Post < ActiveRecord::Base belongs_to :author end class Author < ActiveRecord::Base has_many :posts def name_with_initial "#{first_name.first}. #{last_name}" end end collection_select(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true) 

结果:

  

将选择从实例对象上的调用方法返回的值

call:author_id on:post,@ post是你从控制器传递的

:value_method和:text_method参数是要在集合的每个成员上调用的方法。 返回值分别用作每个标记的值属性和内容。

:id是value_method:name_with_initial是text_method集合用于填充“选项”