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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---