2014-05-08 39 views
0
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})public 

例如,在rails中解釋collection_select 4

collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) 

在這個幫手 - 請解釋一下,什麼是object並詳細method:post是模型名稱,而:author_id是模型中的字段名稱,或者它們是標記名稱。

它們可以是我想要的任何東西,不一定是:postauthor_id,而不是:post123auth_id。它會完全好嗎?

請詳細解釋。在此先感謝

回答

1

我可以給一個簡要的解釋

:post - 你操縱的對象。在這種情況下,它是一個Post對象。

:author_id - 保存帖子時填充的字段。

而且它們應該是這樣的,你不能用stringssymbols來代替它們。

我想你也需要這個

Author.all - 數組您正在使用。

:id - 存儲在數據庫中的值。在HTML方面,這是標籤的值參數

:name_with_initial - 用戶在下拉菜單中看到的輸出。這是<option>標籤之間的值。

希望它有助於!

0
collection_select(
    :post, # field namespace 
    :author_id, # field name 
    # result of this 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 someting like that. 
    # In you 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 row of collection or array 

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

回答這裏Can someone explain collection_select to me in clear, simple terms?由alexkv(此鏈接應幫助你得到的解釋。)