How to setup a default route?

I’ve got a new Rails 2.0.2 application for which I created CRUD functionality with: ruby script/generate scaffold Csv [with a few fields and their types]

http://localhost:3000/csvs runs fine, invoking: app\controllers\ csvs_controller.rb (CsvsController#indexmethod) app\views\csvs\index.html.erb

My goal to make http://localhost:3000 act the same way, but instead it displays:   public\index.html or   Routing Error   No route matches "/" with {:method=>:get} if I remove the index.html file.

config\routes.rb contains: ActionController::Routing::Routes.draw do |map|   map.resources :csvs [snip]   map.connect ':controller/:action/:id'   map.connect ':controller/:action/:id.:format' end

I thought the “map.resources” statement would accomplish my goal. Two questions: 1. What purpose does it serve? 2. How can I achieve my goal?

I’ve looked at the REST online resources and a couple of books but (so far) I can’t get my head around this stuff.

Thanks in Advance, Richard

Hi, Richard

You may re-check the routes.rb again, and you will find one line just like # map.root :controller => "welcome" you can remove the "#" and replace "welcome" with any controller name you want,In your case, it should be "csvs"

Hey Richard,

try

map.index '/', :controller => 'csvs', :action => 'index'

Rick

Hi Guys,

Just after I posted, I found (and adapted the following in The Art of Rails), which met my goal perfectly:   map.home '/', :controller => 'csvs', :action => 'index'

map.index '/', :controller => 'csvs', :action => 'index'

worked fine, too, Rick. Thanks for that

map.root :controller => "csvs"

worked fine, too, Daniel. Thanks for that.

Question: Is that “map.resources :csvs” at the beginning useless, or does it serve some purpose?

Can I shoehorn in a related question?

I want to add another link in index.html.erb. I want to RESTfully invoke load_cvs_filenames in the csvs controller. Is that simple to write?

Best wishes to y'all, Richard

Hey Richard,

Long time no speak...

Question: Is that “map.resources :csvs” at the beginning useless, or does it serve some purpose?

Here's where I find scaffolding most useful - it gives you a picture of how the different RoR components play together.

rails -d postgresql test cd test script/generate scaffold Document title:string synopsis:string body:text author:string

Now you can sniff out one way that MVC splits the processing in RoR just by perusing the newly created files built in support of your Document model.

But, to your specific question:

grep documents config/routes.rb ==> map.resources :documents

When in doubt, rake it out...

rake routes (in /Users/rick/test)               documents GET /documents {:controller=>"documents", :action=>"index"}     formatted_documents GET /documents.:format {:controller=>"documents", :action=>"index"}                         POST /documents {:controller=>"documents", :action=>"create"}                         POST /documents.:format {:controller=>"documents", :action=>"create"}            new_document GET /documents/new {:controller=>"documents", :action=>"new"} formatted_new_document GET /documents/new.:format {:controller=>"documents", :action=>"new"}           edit_document GET /documents/:id/edit {:controller=>"documents", :action=>"edit"} formatted_edit_document GET /documents/:id/edit.:format {:controller=>"documents", :action=>"edit"}                document GET /documents/:id {:controller=>"documents", :action=>"show"}      formatted_document GET /documents/:id.:format {:controller=>"documents", :action=>"show"}                         PUT /documents/:id {:controller=>"documents", :action=>"update"}                         PUT /documents/:id.:format {:controller=>"documents", :action=>"update"}                         DELETE /documents/:id {:controller=>"documents", :action=>"destroy"}                         DELETE /documents/:id.:format {:controller=>"documents", :action=>"destroy"}                                /:controller/:action/:id                                /:controller/:action/:id.:format

map.resources :your_model_pluralized gives you many url_for and path_for routes that can be used to hook up with specific :controller :action [:id]. You'll see examples of use in the scaffold generated controller and views.

Can I shoehorn in a related question?

I want to add another link in index.html.erb. I want to RESTfully invoke load_cvs_filenames in the csvs controller. Is that simple to write?

It's not clear to me what you're after here. Any more thoughts?

Rick

Hi Rick,

grep documents config/routes.rb ==> map.resources :documents When in doubt, rake it out... rake routes

Thanks for the mini-tutorial. I've started getting my routing spec/ usage working, so at the moment I have no outstanding questions. But wait until tomorrow :slight_smile: I just got the beginning of a PayrollApp working with - routes, - xxx_path and, shortly, - create multiple

If you're interested, checkout my routes and controller at http://www.pastie.org/340100.

Best wishes, Richard