[Ruby] How do I add a selection dropdown to a default scaffold form

Harry Dean Hudson Jr. dean at ero.com
Thu Jun 14 19:47:04 PDT 2007


Hi Miene. My experience is mostly with Rails 1.1 and the API may have
changed a bit, but...

On 6/14/07, Bob Marley <mlee1024 at gmail.com> wrote:

> Do I need to replace the <!--[form:watch]-->/<!--[eoform:watch]--> with a <%
> form_for :watches %> / <% end %> ?

No. That should have no effect on what you're trying to do.

>     @productOptions = {"ProductA"=>25, "ProductB"=>32}

[...]

> <p><label for="watch_productid">Productid</label><br/>
> <%= @productOptions.each{|x| print x.to_s } %><br/>  #test to verify hash
> prints (it does)
> <%= form.select(:productid, @productOptions) %>

select() is available to your view code as a helper method,

 http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000506

and I think you want to be passing your options as an Array of Arrays
(where the sub-arrays are of the form [<option name that shows up in
the pull-down>, <value you want that option to submit in your form>]).

Here's a simplified example:

<p>
  <label for="artist_status">Status</label><br/>
  <%=
     options = [["Select a status", ""], ["Active", "active"],
["Inactive", "inactive"]]
     select("artist", "status", options)
  %>
</p>

Maybe obviously, you probably want to use a collect (see the example
on the API page) or the likes to build up your options array...

hope this helps,
dean.


More information about the Ruby mailing list