Patrick Doyle
2008-Jul-14 17:28 UTC
Limit the data displayed in the index view with a select box
Hello, Please be prepared for some very basic, very newbie, very HTML-beginner, knows-nothing-about-AJAX questions... I have a (RESTful, if it makes any difference) application that displays a ton of data (names of documents) on the index page. I would like to limit the amount of data displayed, by adding a SELECT box that narrows the list of data down by selecting a category. Each category has a user defined min & max field, that is used to select rows out of my table. (The category table is also stored in my database). Ideally, I would like to produce a URL that looks like http://site/documents?min=1?max=99, so that it could be bookmarked appropriately. Here''s my question... where do I start? OK, I can narrow that down a little bit... In my index view, I think I need to include a form with a "go" button, but I don''t really think I can use a "form_for" construct, as this is not a form for my documents controller, so I have: <% form_tag(documents_path, {:method => :get}) do %> <%= submit_tag "Go" %> <% end %> I noticed very quickly that I needed the ":method => :get" option, as I ended up creating a blank record the first time I tried this :-) My narrower, more specific questions go like this: a) Is it possible to specify an option to "submit_tag" so that I don''t get "?commit=go" appended to the URL? b) Which of the various select methods should I use to select items from a separate table? c) Is there a way to select a pair of values (min & max) with that one select tag? d) Am I making this more difficult than I need to? --wpd --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Patrick Doyle
2008-Jul-14 20:01 UTC
Re: Limit the data displayed in the index view with a select box
Well, I''ve managed to answer some of my questions, in case anybody else stumbles on this and wonders "what were the answers"...> a) Is it possible to specify an option to "submit_tag" so that I don''t > get "?commit=go" appended to the URL?Yup: <%= submit_tag "Go", :name => nil %>> b) Which of the various select methods should I use to select items > from a separate table?I ended up using "select_tag" and "options_from_collection_for_select" like this: <%= select_tag "category", options_from_collection_for_select(@categories, :id, :description, @category) %>> c) Is there a way to select a pair of values (min & max) with that one > select tag?I punted and decided to return the ID instead of the "min" field. Then I could use that to fetch the min & max fields from the db. It produces URLs that look like http://localhost:3000/documents?category=13 instead of http://localhost:3000/documents?min=0?max=99 So I''m hoping somebody else can give me a pointer into how to do this more cleverly.> d) Am I making this more difficult than I need to?probably. But I''m on to my next question now... in my controller, I have: @categories = Category.all(:order => "min ASC") so that @categories can be passed onto the view as shown above. What I get back is the "id" field from the record. But, 2 lines or so later, I find that I have to do a: @category = params[:category].to_i record = Category.find(@category) and perform a second find on the Category database. This seems wasteful of CPU cycles to me. Perhaps I''m guilty of premature optimization, but it just looks silly to me to query the database once, get back all of the records, and 2 lines later, query it a second time to get back a single record. Instead of using the "id" field of the record in my "options_from_collection_for_select" call, is there some way to extract the index from the collection instead? Then I could just index the array of all of the records I had already queried.> --wpd >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---