I''m trying to display unique counties listed in my database in select box for a property database. I''ve figured out how to do this, but now I can''t figure out how to access the selected value of the select. This mainly has do with the way the HTML select name is outputted. My form code, county is an attribute for my property model: <%= collection_select(:property, :county, @Counties, :county, :county, {:prompt => true}) %> This outputs the HTML <select id="property_county" name="property[county]"><option value="">Please select</option> <option value="Pearl River">Pearl River</option> <option value="Marion">Marion</option> <option value="Stone">Stone</option> <option value="Lamar">Lamar</option> <option value="Forrest">Forrest</option> <option value="Jones">Jones</option> <option value="Washington">Washington</option></select> It is the []''s in the select name "property[county] that is giving me fits. The other items in the search form use select_tag so the output is simply "min_price" rather than "property[min_price]". This is causing a syntax error when I''m trying to put together my search results array in my Property model: def self.find_by_lcc(params) where = [] unless params[:mls].blank? where << "mls = :mls" end unless params[:county].blank? where << "county = :county" end unless params[:min_acreage].blank? where << "acreage >= :min_acreage" end unless params[:max_acreage].blank? where << "acreage <= :max_acreage" end unless params[:min_price].blank? where << "price >= :min_price" end unless params[:max_price].blank? where << "price <= :max_price" end if where.empty? [] else find(:all, :conditions => [where.join(" AND "), params], :order => "city, price desc") end Due to the county problem all the records are being listed rather than just the properties within that county. The browser URL string I''m getting is: public/land?mls=&property[county] =Stone&min_acreage=0&max_acreage=16000000&min_price=0&max_price=1600000&commit=Search I have searched for answers on this for a couple of days and I''m sure it''s a simple syntax method I need to use in compiling my search array. Thanks for any help!
You''re probably better off using select_tag, together with options_for_select here. collection_select appears to be giving you a bit more magic than you want... Example: <%= select_tag :county, options_for_select(["Please select a county...", ""]+@counties.map { |c| [c,c]}) %> (change the entries in the map call if @counties has objects rather than strings - the first entry in the array is the label, and the second is the value that will be sent) --Matt Jones On May 28, 3:23 pm, ReidO <ecr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m trying to display unique counties listed in my database in select > box for a property database. I''ve figured out how to do this, but now > I can''t figure out how to access the selected value of the select. > This mainly has do with the way the HTML select name is outputted. > > My form code, county is an attribute for my property model: > > <%= collection_select(:property, :county, > @Counties, :county, :county, {:prompt => true}) %> > > This outputs the HTML > > <select id="property_county" name="property[county]"><option > value="">Please select</option> > <option value="Pearl River">Pearl River</option> > <option value="Marion">Marion</option> > <option value="Stone">Stone</option> > <option value="Lamar">Lamar</option> > <option value="Forrest">Forrest</option> > <option value="Jones">Jones</option> > <option value="Washington">Washington</option></select> > > It is the []''s in the select name "property[county] that is giving me > fits. The other items in the search form use select_tag so the output > is simply "min_price" rather than "property[min_price]". This is > causing a syntax error when I''m trying to put together my search > results array in my Property model: > > def self.find_by_lcc(params) > where = [] > unless params[:mls].blank? > where << "mls = :mls" > end > unless params[:county].blank? > where << "county = :county" > end > unless params[:min_acreage].blank? > where << "acreage >= :min_acreage" > end > unless params[:max_acreage].blank? > where << "acreage <= :max_acreage" > end > unless params[:min_price].blank? > where << "price >= :min_price" > end > unless params[:max_price].blank? > where << "price <= :max_price" > end > > if where.empty? > [] > else > find(:all, > :conditions => [where.join(" AND "), params], > :order => "city, price desc") > end > > Due to the county problem all the records are being listed rather than > just the properties within that county. The browser URL string I''m > getting is: > > public/land?mls=&property[county] > =Stone&min_acreage=0&max_acreage=16000000&min_price=0&max_price=1600000&com mit=Search > > I have searched for answers on this for a couple of days and I''m sure > it''s a simple syntax method I need to use in compiling my search > array. > > Thanks for any help!
Thank you Matt! This worked nicely. On May 29, 6:26 pm, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You''re probably better off using select_tag, together with > options_for_select here. collection_select appears to be giving you a > bit more magic than you want... > > Example: > > <%= select_tag :county, options_for_select(["Please select a > county...", ""]+...@counties.map { |c| [c,c]}) %> > > (change the entries in the map call if @counties has objects rather > than strings - the first entry in the array is the label, and the > second is the value that will be sent) > > --Matt Jones > > On May 28, 3:23 pm, ReidO <ecr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m trying to display unique counties listed in my database in select > > box for a property database. I''ve figured out how to do this, but now > > I can''t figure out how to access the selected value of the select. > > This mainly has do with the way the HTML select name is outputted. > > > My form code, county is an attribute for my property model: > > > <%= collection_select(:property, :county, > > @Counties, :county, :county, {:prompt => true}) %> > > > This outputs the HTML > > > <select id="property_county" name="property[county]"><option > > value="">Please select</option> > > <option value="Pearl River">Pearl River</option> > > <option value="Marion">Marion</option> > > <option value="Stone">Stone</option> > > <option value="Lamar">Lamar</option> > > <option value="Forrest">Forrest</option> > > <option value="Jones">Jones</option> > > <option value="Washington">Washington</option></select> > > > It is the []''s in the select name "property[county] that is giving me > > fits. The other items in the search form use select_tag so the output > > is simply "min_price" rather than "property[min_price]". This is > > causing a syntax error when I''m trying to put together my search > > results array in my Property model: > > > def self.find_by_lcc(params) > > where = [] > > unless params[:mls].blank? > > where << "mls = :mls" > > end > > unless params[:county].blank? > > where << "county = :county" > > end > > unless params[:min_acreage].blank? > > where << "acreage >= :min_acreage" > > end > > unless params[:max_acreage].blank? > > where << "acreage <= :max_acreage" > > end > > unless params[:min_price].blank? > > where << "price >= :min_price" > > end > > unless params[:max_price].blank? > > where << "price <= :max_price" > > end > > > if where.empty? > > [] > > else > > find(:all, > > :conditions => [where.join(" AND "), params], > > :order => "city, price desc") > > end > > > Due to the county problem all the records are being listed rather than > > just the properties within that county. The browser URL string I''m > > getting is: > > > public/land?mls=&property[county] > > =Stone&min_acreage=0&max_acreage=16000000&min_price=0&max_price=1600000&com mit=Search > > > I have searched for answers on this for a couple of days and I''m sure > > it''s a simple syntax method I need to use in compiling my search > > array. > > > Thanks for any help! > >