Hi I’m a newbie in rails world. Currently I’m using Ruby on Rails in
Aptana studio for developing web application as a part of my course. I
tried and search with all my patience for a solution to have a combo box
in a layout(*.rhtml as base layout for all the pages) which will get
initialised by the data from the database and when I click the search
button with a particular data selected in the combo box, it will show
all the details associated with that data. Its been days now that I’ve
been stuck in this particular thing. So, I would be very grateful for
guidelines or helpful link related with this. Thank you.
On 22 Aug 2008, at 10:26, Jay P. 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
Frederick C. wrote:
On 22 Aug 2008, at 10:26, Jay P. 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
On 22 Aug 2008, at 12:25, Jay P. wrote:
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 C. wrote:
On 22 Aug 2008, at 12:25, Jay P. 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
“IN MODEL”
def self.select_location
@locationname=find_by_sql(“select distinct location from suppliers
order by location”)
@locations=[]
for thelocation in @locationname
@locations << [thelocation.location,thelocation.location]
end
@locations
end
“IN LAYOUT”
Location: <%=select :location,:id,Supplier.select_location,:prompt=>"Select Location"%>
<%= submit_tag 'Search', :class=>'small_button' %> ===============================================================================I get the following error:
ArgumentError in AdminController#show_location
wrong number of arguments (2 for 1)
app/controllers/admin_controller.rb:8:in `show_location’
Request
Parameters:
{“location”=>{“id”=>“Mackay”},
“commit”=>“Search”}
So, please help me where I’m doing wrong and how I can get through it.
Thank you.
On 23 Aug 2008, at 03:37, Jay P. wrote:
=
= ?",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 C. 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
from my database, only “prompt” appears in my combo box.)
=>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.
John Y. 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)
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}
*** 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?
In view:(show_location.rhtml)
Search Results For The Selected Location
<%=suppliers.name%> | <%=suppliers.street_address%> | <%=suppliers.post_code%> | <%=suppliers.phone%> | <%=suppliers.email%> | <%=number_to_currency(suppliers.maximum_price)%> | <%=suppliers.location%> |
But this view just show me the title not the datas. Does this mean my
@datas in controller didn’t get any datas with the find_by_sql method?
How can I find out whats happening and plz help me figure out whats
happening here. Thanks.
On Aug 23, 9:18 pm, Jay P. [email protected]
wrote:
still I’m not clear what should I be putting in the select tag. Here’s
=>the :prompt=> is to make that “select location” appear as default in
the combo box.So, please help me get it correct. Thanks.
Posted viahttp://www.ruby-forum.com/.
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
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.
On Aug 24, 5:14 am, Jay P. [email protected]
wrote:
John Y. 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
I think it kinda give the same,plz correct me if I’m wrong, which is:
=========================================================================== =====
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
Sent from my iPhone
On 24 Aug 2008, at 13:04, Jay P. <rails-mailing-list@andreas-
s.net> wrote:
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…