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

Bob Marley mlee1024 at gmail.com
Sat Jun 16 18:17:31 PDT 2007


Thanks for all the help. I finally figured it out..
Problem:
1) add a dropdown combo box in a web form
2) combobox has to be populated from data in another database

Solution:

1) I got rid of the partial and worked just on the new.rhtml form
<h1>New watch</h1>

<% form_tag :action => 'create' do %>

  <%form_for :watch,
        :url => { :action => 'save', :id => @watch} do |f| %>
        <%= @watch.status %><br/>
        <p><label for="watch_watchdate">Watchdate</label><br/>
        <%= @watch.watchdate  %></p>
....bunch of  scaffold code

        <%= f.select(:productid, @mysql_array) %><br/>

  <%= submit_tag "Create" %>
  <% end %>

<% end %>

<%= link_to 'Back', :action => 'list' %>


To grab the array @mysql_array I did this in the watch_controller.rb
watch_controller.rb
-------------------------------
require 'rubygems'
require 'mysql'


def new
    @watch = Watch.new
    @watch.watchdate = Time.now

  query ="SELECT CONCAT(product_id, ':', name, ' (', codename, ')') AS
product FROM nuke_products WHERE visible = 1 ORDER BY product_id;"
  @mysql_array = Array.new

  with_db do |db|
    res = db.query(query)
    res.each{|row| @mysql_array << row }
    res.free
  end
  end

...scaffolding code

def with_db

    dbh = Mysql.real_connect($MYSQL_HOST, $MYSQL_LOGIN, $MYSQL_PASWD,
$MYSQL_DB)

    begin
        yield dbh
    ensure
        dbh.close
    end
end


end





On 6/14/07, Harry Dean Hudson Jr. <dean at ero.com> wrote:
>
> 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.
> _______________________________________________
> Ruby at zenspider.com - Seattle.rb non-commercial list
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby
>


More information about the Ruby mailing list