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 265725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:47:41+00:00 2026-05-11T22:47:41+00:00

I spent the last three days working on the collection _ select form helper

  • 0

I spent the last three days working on the collection _ select form helper for my “listing” – form, where users can select a category.

I would like to have the category currently set in listing.category_id as the preselected value.

My view code looks like this:

<%= l.collection_select(:category_id, @category, :id, :name, options = {},
                        html_options = {:size => 10, :selected => @listing.category_id.to_s})%>

I know this is not correct, but even reading looking at the explanation from Shiningthrough (http://shiningthrough.co.uk/blog/show/6) I can not understand how to proceed.

Thank you for your support,

Michael

View:
as above
Controller:

def categories #Step 2
@listing = Listing.find(params[:listing_id])
@seller = Seller.find(@listing.seller_id)
@category = Category.find(:all)
@listing.complete = "step1"

respond_to do |format|
  if @listing.update_attributes(params[:listing])
    flash[:notice] = 'Step one succesful. Item saved.'
    format.html #categories.html.erb
end
end
end
  • 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-11T22:47:41+00:00Added an answer on May 11, 2026 at 10:47 pm

    collection_select doesn’t support the selected option, in fact, it doesn’t need it.
    It automatically selects the option whose value matches the value of the form builder object.

    Let me show you an example. Assuming each post belongs to a category.

    @post = Post.new
    
    <% form_for @post do |f| %>
      <!-- no option selected -->
      <%= f.collection_select :category_id, Category.all, :id, :name, :prompt => true  %>
    <% end %>
    
    @post = Post.new(:category_id => 5)
    
    <% form_for @post do |f| %>
      <!-- option with id == 5 is selected -->
      <%= f.collection_select :category_id, Category.all, :id, :name, :prompt => true  %>
    <% end %>
    

    EDIT:

    I’d suggest to use representative variable names. Use @categories instead of @category. 🙂
    Also, split the update logic from the read-only view.

    def categories #Step 2
      @listing = Listing.find(params[:listing_id])
      @seller = Seller.find(@listing.seller_id)
      @categories = Category.find(:all)
      @listing.complete = "step1"
    
      respond_to do |format|
        if @listing.update_attributes(params[:listing])
          flash[:notice] = 'Step one succesful. Item saved.'
          format.html #categories.html.erb
        end
      end
    end
    
    <% form_for @listing do |f| %>
      <%= f.collection_select :category_id, @categories, :id, :name, :prompt => true %>
    <% end %>
    

    If it doesn’t work (that is it selects the prompt) it means either you don’t have a category_id associated to that record or the Category collection is empty. Be sure to not reset the value of category_id for @listing somewhere before the object is passed to the form.

    EDIT 2:

    class Category
      def id_as_string
        id.to_s
      end
    end
    
    <%= f.collection_select :category_id, Category.all, :id_as_string, :name, :prompt => true  %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent last 3 days until I got gps working on android using
I spent the last three days trying to create a small app using SDL
I'm sure this has been answered before, but I've spent the last three hours
I spent the last year working on a project that involved mostly java for
I've spent the last few days banging my head and searching Google and have
I have spent the last several years fighting tooth and nail to avoid working
I have spent the last few days attempting to integrate a Grails (version 1.3.2)
I've spent two days on this and I still can't find the cause of
I've spent the last couple days reading my various books and looking through MSDN's
I've spent so much time the last few days trying to work out some

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.