# Limit the data displayed in the index view with a select box

**URL:** <https://www.ruby-forum.com/t/limit-the-data-displayed-in-the-index-view-with-a-select-box/143044>\
**Category:** Rails\
**Created:** [July 14, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/limit-the-data-displayed-in-the-index-view-with-a-select-box/143044 "2008-07-14T19:28:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [July 14, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/limit-the-data-displayed-in-the-index-view-with-a-select-box/143044/1 "2008-07-14T19:28:46Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [July 14, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/limit-the-data-displayed-in-the-index-view-with-a-select-box/143044/2 "2008-07-14T22:03:53Z")

</div>

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](http://localhost:3000/documents?category=13)

instead of

[http://localhost:3000/documents?min=0?max=99](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.
