How to add a value to a select field in Ruby On Rails

I’ll just leave this here: When you fill a select box directly from a array returned by .find(:all) you can add more values tothe select box this way:

Controller:

    @hosts = Host.find(:all).collect {|p| [p.name, p.id] }
    @hosts << ["None", nil]

View:

    <dd>
      <%= f.select :linkedhost, @hosts %>
      <%= error_message_on(:service, :linkedhost) %>
    </dd>
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • LinkedIn
  • Spurl
  • StumbleUpon
  • Technorati
  • Yigg

Tags: , , , , ,

Leave a Reply