load data from database in a combo box

Have you checked out the api docs for collection_select, select_tag, options_for_select ?

Fred

Frederick Cheung wrote:

guidelines or helpful link related with this. Thank you.

Have you checked out the api docs for collection_select, select_tag,
options_for_select ?

Fred

thanks fred for the quick reply. to be honest i'm so new that i'vent yet
even got chance to go thru api docs. but, dont get me wrong, i've been
going thru agile web development book and some basic ruby tutorials for
'round a week now. btw, i got the code working and now i can get data
from database in my combo box. Few questions though: is ther any code
like @products=product.find(:all,:order=>product), where i can give
option like distinct in sql to only retrive distinct data? how can i
link it to a button so that when i click the search button, all the data
related to the selected data in combobox will appear? I mean how can i
retrive the selected data in the combobox like combo.getSelectedItem()
in java here in rails so that i can use it later to get what i want.
Finally, is there downloadable version of api docs coz i find it bit
irritating specially when the internet is slow. thanks

Frederick Cheung wrote:

guidelines or helpful link related with this. Thank you.

Have you checked out the api docs for collection_select, select_tag,
options_for_select ?

Fred

thanks fred for the quick reply. to be honest i'm so new that i'vent
yet
even got chance to go thru api docs. but, dont get me wrong, i've been
going thru agile web development book and some basic ruby tutorials
for
'round a week now. btw, i got the code working and now i can get data
from database in my combo box. Few questions though: is ther any code
like @products=product.find(:all,:order=>product), where i can give
option like distinct in sql to only retrive distinct data? how can i

Sure. the :select option to find allows you to fiddle with all that.

link it to a button so that when i click the search button, all the
data
related to the selected data in combobox will appear? I mean how can i
retrive the selected data in the combobox like combo.getSelectedItem()
in java here in rails so that i can use it later to get what i want.

Well you'll end up doing a form post (normal or ajax) so it will be
somewhere in the params hash.

Finally, is there downloadable version of api docs coz i find it bit
irritating specially when the internet is slow. thanks

The doc is already installed locally. Run gem server and connect to
localhost:8808 to see the docs for all your gems.
Or from your rails app run rake doc:rails which will generate the docs
in one chunk (rather than having the docs separatelu for each of the
gems making up rails)

Frederick Cheung wrote:

Frederick Cheung wrote:

Well you'll end up doing a form post (normal or ajax) so it will be
somewhere in the params hash.

So, I've tried coding the following in my controller and in my layout:
"IN CONTROLLER"

=

=

=

=

======================================================================
def show_location
   @datas=Supplier.find_by_sql("select * from Suppliers where location
= ?",params["details"])
end

If you're going to use find_by_sql like that then you need to pass a
single array ie

Supplier.find_by_sql(["select * from Suppliers where location
= ?",params["details"]])

Also from your error message you ca see that params["details"] is not
right.

Fred

Frederick Cheung wrote:

Also from your error message you ca see that params["details"] is not
right.

Fred

Thanks fred for being so helpful. As I told you I'm just a new kid in
the town of ruby, I'm a bit confused with certain things and I don't if
its my weakness in english or what, I just don't seems to understand the
api docs that clearly either. I looked in the api docs for "select" but
still I'm not clear what should I be putting in the select tag. Here's
what I've done.

<%=select :location,:id,Supplier.select_location,{:prompt=>"Select
Location"}%>

where:
=>:location is a column in the table "suppliers".

=>:id (I just blindly put it there. Coz if I don't, instead of locations

=>Supplier.select_location (here as to make data appear in the combo box
I had to put key/value in the hash and I was just extracting locations
from the table I did:

    for thelocation in @locationname
      @locations << [thelocation.location,thelocation.location]
    end

which has, in my knowledge, made the hash table with location as both
key and value.

=>the :prompt=> is to make that "select location" appear as default in
the combo box.

So, please help me get it correct. Thanks.

Maybe I'm missing something here, but I think all you need to do is
something similar to this:

   <%= collection_select :whatever, :state_id,
State.find(:all), :id, :name_abbr %>

In this case, I've got a table with all the states, with the columns
id, name, and name_abbr. The collection_select method here will
create a select menu with values from the name_abbr column in the
select menu and the id sent as the state_id parameter.

You'll end up sending a hash looking like
{:whatever => {:state_id => :id}}

Here are the api docs for collection_select
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001753

John Yerhot wrote:

Maybe I'm missing something here, but I think all you need to do is
something similar to this:

   <%= collection_select :whatever, :state_id,
State.find(:all), :id, :name_abbr %>

Thanks for the help, but as I'm new, its kinda pushing me toward more
confusion for the fact that I have to have :name_abbr method declared.
Ahhhh I don't know wat.

You'll end up sending a hash looking like
{:whatever => {:state_id => :id}}

Btw, in select also I'm using this:

<%=
select("Selected","location_name",Supplier.find(:all).collect{|s|[s.location,s.location]},{:prompt=>"Select
Location"}) %>

I think it kinda give the same,plz correct me if I'm wrong, which is:
{Selected=>{location_name=>location}

**The reason I'm doing [s.location,s.location] is that if I put id, I
can't get distinct location and moreover, I have to do the search base
on location. All appears fine, my combo box has location names from db
and are distinct. But when I select a location name and hit search I get
nothing, not even error, just the title which I've set for that
particular page. Below are the code:

In controller:(admin_controller.rb)

John Yerhot wrote:
> Maybe I'm missing something here, but I think all you need to do is
> something similar to this:

> <%= collection_select :whatever, :state_id,
> State.find(:all), :id, :name_abbr %>

Thanks for the help, but as I'm new, its kinda pushing me toward more
confusion for the fact that I have to have :name_abbr method declared.
Ahhhh I don't know wat.

First off (and this is the same as with select), this assumes that you
have an instance variable @whatever with a method (or attribute, it's
the same thing) called state_id which is the currently selected state.
If you don't have such an instance variable then you'd be better off
use select_tag and options_for_select or
options_from_collection_for_select. the last two arguments to this
function are methods to call. The first is for the value that should
be sent to you controller (typically id). The second is for generating
the text to display to the user. This could be the same as the first
one but typically it is some human readable description. In your case
one could pass :id and :location. Of course if you do want both to be
location then just pass :location twice

> You'll end up sending a hash looking like
> {:whatever => {:state_id => :id}}

Btw, in select also I'm using this:

<%=
select("Selected","location_name",Supplier.find(:all).collect{|s|[s.locatio n,s.location]},{:prompt=>"Select
Location"}) %>

I think it kinda give the same,plz correct me if I'm wrong, which is:
{Selected=>{location_name=>location}

**The reason I'm doing [s.location,s.location] is that if I put id, I
can't get distinct location and moreover, I have to do the search base
on location. All appears fine, my combo box has location names from db
and are distinct. But when I select a location name and hit search I get
nothing, not even error, just the title which I've set for that
particular page. Below are the code:

In controller:(admin_controller.rb)
=========================================================================== =====
def show_location
@datas=Supplier.find_by_sql(["select * from suppliers where location
= ?",params["details"]])
end
=========================================================================== =====
*** if I'm not wrong params["details"] should look like this isn't it?:
{Selected=>{location_name=>location}

Yes you are wrong. If you look at the log for your application you can
see exactly what the parmas hash is. You'll see that nowhere does the
word details appear and hence params['details'] is nil. Or to put
things another way, nowhere else in your code does 'details' appear.
How could rails know to submit the parameter under that name.

if the hash looks like {"Selected"=>{"location_name"=> "Mackay"}} then
it should be clear that you need params["selected"]["location_name"]}.
If the params look like something else then adjust appropriately

*** And that my sql statement should take just the location from above
and give me all the details of that particular location, isn't it?

Should word, but there's really do reason to use find_by_sql here.

Fred

Thanks Fred ,you have been really very helpful. Now I can qery my
database and extract the datas to my page of that particular location.

Should word, but there's really do reason to use find_by_sql here.

Fred

Do you mean, "Should work, but there's really no reason to use
find_by_sql here." If so, then what can I use? I was trying to use
"find" with :conditions attribute but couldn't figure out how to put
them in a code.

Thanks Fred ,you have been really very helpful. Now I can qery my
database and extract the datas to my page of that particular location.

Should word, but there's really do reason to use find_by_sql here.

Fred

Do you mean, "Should work, but there's really no reason to use
find_by_sql here." If so, then what can I use? I was trying to use
"find" with :conditions attribute but couldn't figure out how to put
them in a code.

Yup. The limits of the iPhone typo corrector

:conditions => { :foo => "bar"}

Or

:conditions => ["foo = ?", "bar"]

Should both do the trick

Fred

:conditions => { :foo => "bar"}

Or

:conditions => ["foo = ?", "bar"]

Should both do the trick

Fred

You are great man!!! Thanks now I'm up and running...