Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:23:31+00:00 2026-05-28T05:23:31+00:00

I am going through the Rails API docs for collection_select and they are god-awful.

  • 0

I am going through the Rails API docs for collection_select and they are god-awful.

The heading is this:

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

And this is the only sample code they give:

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

Can someone explain, using a simple association (say a User has_many Plans, and a Plan belongs to a User), what I want to use in the syntax and why?

Edit 1: Also, it would be awesome if you explained how it works inside a form_helper or a regular form. Imagine you are explaining this to a web developer that understands web development, but is ‘relatively new’ to Rails. How would you explain it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:23:32+00:00Added an answer on May 28, 2026 at 5:23 am
    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.
    )
    

    Or your example can be represented as the following code:

    <select name="post[author_id]">
        <% Author.all.each do |author| %>
            <option value="<%= author.id %>"><%= author.name_with_initial %></option>
        <% end %>
    </select>
    

    This isn’t documented in the FormBuilder, but in the FormOptionsHelper

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going through a video tutorial that was using Rails 2.3, and they
I was just going through this old rails cast episode, and one thing it
I'm new to creating a rails application. I'm going through this tutorial , and
I've been going through some good (seeming) resources for Rails tutorials, and will dutifully
Going through Javascript documentation, I found the following two functions on a Javascript object
Going through the microsoft authentication tutorial listed here they have you create a master
Going through Lynda's 2010 tutorial on rails and have been stuck on migration for
im going through the setup on http://www.railstutorial.org/chapters/static-pages#fig:autotest_green and im stuck on this particular error:
I'm in the final stages of going round trip through the entire Rails cycle:
tutorial: http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/ I am going through the CloudEdit rails/backbone.js tutrorial and got stuck on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.