small medium large xlarge

21 Jan 2008, 16:10
Don Bessinger (8 posts)


I’ve been messing around with routing, particularly defining routes with map.connect. Rails spits out a generic home page for an application, found in /public.index.html. I’d like to use routing to set my application’s home page to a login page. Here’s what I’ve done:

  1. I’ve added this line to the application’s config/routes.rb file. It is the first line after the WSDL line (right after the Rails-generated # Install the default route as the lowest priority. comment), so the file looks like this:
ActionController::Routing::Routes.draw do |map|

  # [bunch of comments here]

  # Allow downloading Web Service WSDL as a file with an extension
  # instead of a file named 'wsdl'
  map.connect ':controller/service.wsdl', :action => 'wsdl' 

  # Install the default route as the lowest priority.
  map.connect '/', :controller => 'user', :action => 'login'
  map.connect ':controller/:action/:id.:format'
  map.connect ':controller/:action/:id'
  1. Invoked script/console and made sure the updated routes were loaded by entering this command:
>> load "config/routes.rb"
=> []
  1. Tested my new and improved routing by doing this at the console:
>> rs = ActionController::Routing::Routes
=> #<ActionController::Routing::Routset:0x2....[the expected mountain of output here]
>> rs.recognize_path '/'
=> {:action=>"login", :controller=>"user"}

OK, I’m satisfied with that. However, when I fire up my browser (Firefox 2) and hit http://localhost:3000/, I still get the Rails-generated public/index.html page. I’ve tried closing all browser instances, stopping and restarting the WEBrick server, and then hitting the site, but meet with the same result. Anybody have any thoughts on what gives? I’ve also tried deleting my new line and adding default controller and action values to the entries that Rails generated, but that didn’t work either (in fact, it generated warnings when I reloaded the config/routes.rb from the console). Thanks!

Don Bessinger

24 Jan 2008, 20:01
Don Bessinger (8 posts)

OK, I’ve answered part of my own question, anyway. If my suspicions are correct, this line:

map.connect '/', :controller => 'user', :action = 'login'

is the opposite of what I want to do. I’ve apparently made sure that, when a user hits http://localhost:3000/user/login, he is promptly routed to http://localhost:3000/ - certainly not the intended result.

Still not sure how to get an RHTML index page up and running, though, so if anyone has any thoughts, I’m all ears.

Don Bessinger

29 Feb 2008, 02:32
Timothy Knight (5 posts)

Hey Don, I don’t know if I’m over simplifying your question… and perhaps after a month you have already found an adequate solution, but…

Perhaps have a main page in which my typical root route might look like:

@map.connect ‘’, :controller => “welcome”@

And in the index definition of the welcome controller I would first check to see if the user is logged in - if not, the send them to the user controller login action.

Also, be sure that you have removed the index.html file from the public folder. If this file is found the dispatcher won’t go looking into the controllers for anything it will stop at the index.html file and render that.

Tim Knight

01 Mar 2008, 17:06
Don Bessinger (8 posts)

Thanks, Tim! Actually, I haven’t solved the problem. Instead, I just used regular HTML redirects to show the usual “This page has moved. If you are not redirected…” message. I’ll give this a try as soon as I figure out the answer (or workaround!) to my current problem. Thanks again.

Don Bessinger

01 Mar 2008, 19:00
Don Bessinger (8 posts)

Bingo. My error was including the slash character in map.connect’s path parameter. Lesson learned, and thanks again, Tim.

Don Bessinger

06 Mar 2008, 03:39
Timothy Knight (5 posts)

Awesome Don, glad it helped.

You must be logged in to comment