Forum: Ruby on Rails Rails: Select and html forms

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe E. (Guest)
on 2005-12-15 19:20
I'm close to getting this to work, but not quite there.  I'm trying to
implement a dropdown control, with the current choice selected.
I have the following code in the view:

      5 <select id="product_product_type" name="product[product_type]">
      6 <%=
      7   types = ProductType.find(:all, :order => "product_type")
      8   options_from_collection_for_select( types, :id, :product_type,
selected=@product_type_id)
      9 %>
     10 </select

Which renders the following:

<label for="product_product_type">Product type</label><br/>
<select id="product_product_type" name="product[product_type]">
<option value="2"></option>
...
<option value="6" selected="selected">Supplies</option>
</select>


However, in the browser the "Supplies" item isn't displaed on load, but
the blank first option is.  Anyone see what I'm doing wrong?
jschairb (Guest)
on 2005-12-15 21:28
Joe,

I found this thread on another list, I'm working on it now...I'll let
you know
if I come up with a working solution.

http://wrath.rubyonrails.org/pipermail/rails/2005-...
jschairb (Guest)
on 2005-12-15 21:36
jschairb wrote:
> Joe,
>
> I found this thread on another list, I'm working on it now...I'll let
> you know
> if I come up with a working solution.
>
> http://wrath.rubyonrails.org/pipermail/rails/2005-...

This works, it's probably not the best implementation...but it works.

http://wiki.rubyonrails.com/rails/pages/HowtoUseFo...

look for the section called 'A collection_select like helper'  I copied
that code into application_helper.rb and then used his view
implementation.  Worked for me, although I had to change the last part
in the view from :

@page.section_id

to just

page.section_id


Hopefully this will get you going.  Later.
Sandro Duarte (Guest)
on 2005-12-15 23:20
(Received via mailing list)
Try this:

<%= collection_select(:product, :product_type, types, :id,
:product_type, { :include_blank => true }) %>

It works fine for me.

SandroDS


On 12/15/05, Joe E. <removed_email_address@domain.invalid> wrote:
>      10 </select
>
> However, in the browser the "Supplies" item isn't displaed on load, but
> the blank first option is.  Anyone see what I'm doing wrong?
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Sandro Duarte
Analista de Sistemas
TRE-RS/SI
This topic is locked and can not be replied to.