# Default AS show behavior

**URL:** <https://www.ruby-forum.com/t/default-as-show-behavior/130645>\
**Category:** Rails\
**Created:** [March 1, 2008, 4:32am UTC](https://www.ruby-forum.com/t/default-as-show-behavior/130645 "2008-03-01T04:32:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![agilehack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@agilehack](https://www.ruby-forum.com/u/agilehack)\
**Post date:** [March 1, 2008, 4:32am UTC](https://www.ruby-forum.com/t/default-as-show-behavior/130645/1 "2008-03-01T04:32:59Z")

</div>

I am new to AS and probably using it in the simplest manner but…by  
default when I invoke my controller- localhost:3000/mycontroller the  
default behavior is to show all the data for the model I am tied to.  
(kind of like select \* from table is running on page load?) I would  
rather not run/load/display any rows until the user has searched for  
something, how do I change this behavior? Essentially only the search  
button would be visible at first. I am trying to use this for a  
commercial webapp and I cannot display the full set of data as soon as  
the user opens the page, only after they have explicitly searched.  
ALSO once a search has been submitted, if the result is only one  
record can I change the default behavior to use the ‘show’ and skip  
the list?

Please help, thank you

---

<div class="post-metadata">

**Author:** ![agilehack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@agilehack](https://www.ruby-forum.com/u/agilehack)\
**Post date:** [March 1, 2008, 10:21am UTC](https://www.ruby-forum.com/t/default-as-show-behavior/130645/2 "2008-03-01T10:21:33Z")

</div>

agilehack wrote:

> something, how do I change this behavior? Essentially only the search  
> button would be visible at first. I am trying to use this for a  
> commercial webapp and I cannot display the full set of data as soon as  
> the user opens the page, only after they have explicitly searched.  
> ALSO once a search has been submitted, if the result is only one  
> record can I change the default behavior to use the ‘show’ and skip  
> the list?

In config/routes.rb set the default route (’’) to map to your #index  
method and remove (or rename) public/index.html if this search should be  
the main entry point for the application.

Add the #search method name to the resource route for the item being  
searched by something like:

map.resources :items, :collection =\> { :search =\> :post }

This says to add a route for /items/search and map it to the #search  
method. This only accepts POST requests. (You should find that  
“map.resources :items” is already there if your are using RESTful  
routes, otherwise if you are not using RESTful routes, you don’t need to  
update your routes.rb file.)

Update the #index method and views (copy the “index” view to a “search”  
view for later) to display the search input form with #search as the  
action.

If the input item of the form is named, say, “search\_item”, then your  
#search method logic can be something like:

if params[:search\_item]  
@items = Item.find\_by\_search\_column(params[:search\_item])  
if @items.length == 1  
@item = @items[0]  
render :action =\> :show  
else  
respond\_to do |format|  
format.html # search.html.erb  
format.xml { render :xml =\> @items }  
end  
end  
else  
redirect\_to :action =\> :index  
end

Here, if you have no search item, you’ll redirect to the index to show  
the search input. Otherwise you search for the items. If there is 1,  
then use the same instance variable the #show method sets and render the  
“show” view, otherwise render your “search” view (which will look like  
the original “index” view).

Further development: do something useful if no items are found.

There are probably better ways to do this, but this should work ok.
