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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:04:53+00:00 2026-05-24T07:04:53+00:00

I have a simple dropdown that I want to populate from a model. I

  • 0

I have a simple dropdown that I want to populate from a model. I don’t want to bind it to another model at all, just a simple standalone form with a list of items and handle storing the state of the dropdown in a session variable, I can achieve it with a more brute force approach as shown but it doesn’t feel very ‘rails’ to me.

<form action='/home/switch' method='post'>
    <select name="all_items">
    <% @items.each do |i| %>
        <option value="<%= i.id %>" <%= i.id.to_s == session[:current_item] ? "selected" : "" %>><%= i.name %></option>
    <% end %>
    </select>
    <input type="submit">
</form> 

Is there a better way to do this in Rails?

Update: Yep. collection_select worked for me:

<%= collection_select(:item, :id, Item.all, :id, :name, {:selected => session[:current_item].id}) %>
  • 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-24T07:04:54+00:00Added an answer on May 24, 2026 at 7:04 am

    Take a look at form_tag, select_tag, options_from_collection_for_select, and/or collection_select.

    So your example might look like this (not tested, may have typos)

    <%= form_tag('/home/switch') do %>
    <%=   select_tag('all_items', options_from_collection_for_select(@items, 'id', 'name')) %>
    <%=   submit_tag %>
    <%= end %>
    

    This is missing the “selected” bit, take a look at the docs for that.

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

Sidebar

Related Questions

I want to have a simple select->option dropdown list that I am not passing
In my ASP.net MVC View I have a dropdown that I want to get
I've got a simple dropdown menu that I want to hide whenever you click
I have a simple html option/select (dropdown) menu. I want to use JQuery to
I have a simple dropdown box and jquery to get value from what i
I have simple win service, that executes few tasks periodically. How should I pass
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have simple SSIS package which reads data from flat file and insert into
I have simple SSIS package where I import data from flat file into SQL
I have this wordpress blog that has many pages with subpages that dropdown 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.