ruby collection_select描述

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby collection_select描述相关的知识,希望对你有一定的参考价值。

collection_select(
    :post, # field namespace 
    :author_id, # field name
    # result of these two params will be: <select name="post[author_id]">...

    # then you should specify some collection or array of rows.
    # It can be Author.where(..).order(..) or something like that. 
    # In your example it is:
    Author.all, 

    # then you should specify methods for generating options
    :id, # this is name of method that will be called for every row, result will be set as key
    :name_with_initial, # this is name of method that will be called for every row, result will be set as value

    # as a result, every option will be generated by the following rule: 
    # <option value=#{author.id}>#{author.name_with_initial}</option>
    # 'author' is an element in the collection or array

    :prompt => true # then you can specify some params. You can find them in the docs.
)

以上是关于ruby collection_select描述的主要内容,如果未能解决你的问题,请参考以下文章

Ruby on Rails - 在填写表单时更新 collection_select

使用 collection_select

如何将两个属性作为 text_method 传递给 rails 中的 collection_select

在 rails collection_select 中使用短日期时间

如何在 Rails 中为 collection_select 设置 HTML 选项?

collection_select 和 has_and_belongs_to_many 关系