views:

98

answers:

2

My rails version is 2.3.5(2.3+)

How can I visit urls with .html suffix? Just like localhost:3000/welcome.html (welcome is a controller).

I got routing errors when I visit urls above.But it works if the url with format param like this:

localhost:3000/welcome?format=html

In routes.rb:

ActionController::Routing::Routes.draw do |map|
  map.root :controller => "welcome"
  map.resources :users
  map.resource :session

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

But but but I found localhost:3000/users.html works.

+1  A: 

Use this route to connect to an controller:

map.connect "welcome.html", :controller => :welcome, :action => :index

Whether there is .html does not matter for routing purposes, it is just like any other path connecting to any other controller. So no need to modify your controller for this.
Using the format=html results in a parameter, so a controller can return the specific type of result, which is not what you want according to your question.

According to your information this (allowing .html in your paths) is automatically implemented when creating routes with the map.resources method. Since it is working for users in your example.

Veger
That is to say:if it's not a restful resource(like map.resources :users),I must config named routes for each format request from all kinds of clients(pc,mobile,etc)html,wml,etc.now How could it auto display a view page by its suffix(.html, .wml), just restful
qichunren
How do you want restful URLs? For users it would be ocme something like `sers.html` for an index action and `users.html/10` and `users.html/10/edit` for a show and edit action? That does not seem right...
Veger
+1  A: 

You could try this:

map.connect ':controller.:format', :action => :index
mikezter