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

  • Home
  • SEARCH
  • 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 930899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:24:49+00:00 2026-05-15T20:24:49+00:00

I have a select dropdown where I generate the options from database entries, then

  • 0

I have a select dropdown where I generate the options from database entries, then add an option to the beginning like:

@select = Service.find_services_by_id(id).collect { |p| [p.name, p.id] }
@select.unshift( [ "Choose a service", 0] )

Then in the HAML view I have:

=select_tag "service_id", options_for_select(@select)

But I’m looking at refactoring that either by:

1) Adding another method to the model which also does the unshift operation to return to me all the data for the select in one piece

2) Moving the whole @select definition to a view helper and calling it from the view

3) Just have that first @select line in the controller, then have a view helper do the ‘unshift’ part

But I’m having trouble figuring out what makes the most sense. Thoughts?

  • 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-15T20:24:50+00:00Added an answer on May 15, 2026 at 8:24 pm

    I’d go for 3rd, but not only do the unshift in the helper, but create whole select thingie there too:

    module SomeHelper
      def service_select(objects)
        select_tag "service_id", options_for_select(objects.unshift(["Select a service", 0]))
      end
    end
    

    Then in view you’d just call =service_select(@select).

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

Sidebar

Related Questions

I have an issue regarding to auto populating a select dropdown from jQuery/JSON data
I have a little dropdown menu similar <select> , which contolled from external js-function.
So I have a dropdown list <select id=theSelectId> <option value=volvo>Volvo</option> <option value=saab>Saab</option> <option value=mercedes>Mercedes</option>
if i have a dropdown with the following html: <select id=myDropdown name=myDropdown> <option value=6>Six</option>
I have a dropdown <select> field which gets its values from the db. I'm
currently i have a form, that generates a Drop-Down-Select from my category-entity: $builder ->add('category',
I have this dropdown (select) menu generated with JQuery, dynamically using JSON coming from
i have a time dropdown that shows hour in one select, minutes in another
I have this HTML dropdown: <form> <input type=text id=realtxt onkeyup=searchSel()> <select id=select name=basic-combo size=1>
Ok here goes: I have a SELECT drop down option with US state names

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.