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

for e.g.

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

In this helper - Please explain, What is object and method in detail? Is :post a model name and :author_id a fieldname in a model or are they tag name.

can they be anything I want, not necessarily :post and author_id, instead :post123 or auth_id. Will it be perfectly ok?

Please explain be in detail. thanks in advance

Pavan
  • 33,316
  • 7
  • 50
  • 76
kamal
  • 996
  • 15
  • 25

2 Answers2

1

I can give a brief explanation

:post - The object you are manipulating. In this case, it's a Post object.

:author_id - The field that is populated when the Post is saved.

And they are supposed to be like that.you can't replace those with any strings or symbols.

I think you also need this

Author.all - The array you are working with.

:id - The value that is stored in the database. In terms of HTML, this is the tag's value parameter

:name_with_initial- The output that the user sees in the pull-down menu. This is the value between the <option> tags.

Hope it Helps!

Pavan
  • 33,316
  • 7
  • 50
  • 76
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.
)

answered here Can someone explain collection_select to me in clear, simple terms? by alexkv (This link should help you to get explanation.)

Community
  • 1
  • 1
Dave
  • 4,376
  • 3
  • 24
  • 37