0

I've just upgraded to Rails 3, and decided to take it for a whirl with a dummy/sandbox app. The strange thing is I can't seem to get any routes to work!

Here's my problem:

[cobychapple@shiva:Dev]$rails new TestApp
      create  
      create  README
      create  Rakefile
      ...
[cobychapple@shiva:Dev]$cd TestApp/
[cobychapple@shiva:TestApp]$rails g scaffold widget name:text
      invoke  active_record
      create    db/migrate/20101020115518_create_widgets.rb
      create    app/models/widget.rb
      ...
[cobychapple@shiva:TestApp]$rake db:migrate
(in /Users/cobychapple/Dev/TestApp)
==  CreateWidgets: migrating ==================================================
-- create_table(:widgets)
   -> 0.0015s
==  CreateWidgets: migrated (0.0016s) =========================================

[cobychapple@shiva:TestApp]$rake routes
(in /Users/cobychapple/Dev/TestApp)
    widgets GET    /widgets(.:format)          {:action=>"index", :controller=>"widgets"}
    widgets POST   /widgets(.:format)          {:action=>"create", :controller=>"widgets"}
 new_widget GET    /widgets/new(.:format)      {:action=>"new", :controller=>"widgets"}
edit_widget GET    /widgets/:id/edit(.:format) {:action=>"edit", :controller=>"widgets"}
     widget GET    /widgets/:id(.:format)      {:action=>"show", :controller=>"widgets"}
     widget PUT    /widgets/:id(.:format)      {:action=>"update", :controller=>"widgets"}
     widget DELETE /widgets/:id(.:format)      {:action=>"destroy", :controller=>"widgets"}

[cobychapple@shiva:TestApp]$rails s -d
=> Booting WEBrick
=> Rails 3.0.1 application starting in development on http://0.0.0.0:3000
[cobychapple@shiva:TestApp]$

So now I head on over to my browser and visit http://localhost:3000/widgets

Rails says: Routing Error: No route matches "/widgets"

I don't get how rake routes can show the route exists and then the server won't match it. I've googled a whole bunch and there seems to be a few people with routing issues in rails 3, but none of them are as apparently basic as what I'm getting. I'm sure I've just missed something small along the way but I can't for the life of me figure it out! Any suggestions?

My routes.rb file is only what that scaffold generates + documentation comments:

TestApp::Application.routes.draw do
  resources :widgets

  # The priority is based upon order of creation:
  # skip the rest of the comments...
end
3
  • Hav you the file app/controller/widgets_controller.rb ?
    – shingara
    Oct 20, 2010 at 12:18
  • I made exactly the same steps that you did and it worked without problems. Weird...
    – Matt
    Oct 20, 2010 at 13:45
  • Yep... app/controllers/widgets_controller.rb is present and as-generated by the scaffold script. Thanks for the check tho!
    – Coby
    Oct 20, 2010 at 21:35

4 Answers 4

3

You need to restart the server after you modify the routes. As you have said in your answer, you have started the server with -d option, so it went into the background, and then you have tried to run it again - but the first one was still working. You should have killed the first server.

That background process you mention, was not any rogue process, it was your first server.

The answer is: 1) don't start the server with '-d' option in development (so it will be easier to restart or kill it later) 2) always restart your server after you change the routes file (or any file in config/initializers)

0

I did exactly the same thing and it worked for me. The only thing I did different was use mysql database. Why don't you delete the application and do it over again. I can't imagine what you missed.

These are my steps:

rails new TestApp -d mysql
cd TestApp/
rails g scaffold widget name:text 
rake db:create
rake db:migrate
rake routes
(in /home/gjp/work/TestApp)
    widgets GET    /widgets(.:format)          {:controller=>"widgets", :action=>"index"}
    widgets POST   /widgets(.:format)          {:controller=>"widgets", :action=>"create"}
 new_widget GET    /widgets/new(.:format)      {:controller=>"widgets", :action=>"new"}
edit_widget GET    /widgets/:id/edit(.:format) {:controller=>"widgets", :action=>"edit"}
     widget GET    /widgets/:id(.:format)      {:controller=>"widgets", :action=>"show"}
     widget PUT    /widgets/:id(.:format)      {:controller=>"widgets", :action=>"update"}
     widget DELETE /widgets/:id(.:format)      {:controller=>"widgets", :action=>"destroy"}
rails s

And then http://localhost:3000/widgets worked.

0

So it appears I've solved the problem (but I still don't get it!):

What I was doing was running the server script using the -d (detached mode) which returns you straight back to the console. I tried running the server without the -d option to see what results the server activity log was saying, and when I ran the server script this way it crashed straight up saying the port was already in use (even though with run detatched it didn't crash?!?!?).

Rebooted. Everything worked as normal and the server script runs fine again in either mode.

So I guess there must have been some background port binding issue that was wiped clean with a restart. If this is happening to anyone else in the future, my guess is you might have some rogue process (MAMP bug? rogue mongrel server from another rails app?) bound to your hosts port somewhere. Check for that, and if you can't find anything reboot and see if it fixes it.

shrug

Thanks for your input tho guys and/or gals.

0

In a correctly operating development environment it should not be necessary to restart the server to reload the routes.

Ignoring that - because you booted your 2nd server attempt in detached mode you didn't see the binding error it received.

It's typically easier to run your servers in the foreground so you can see any errors.

If you do get a binding error, then you can track it down:

sudo netstat -an | grep LISTEN | grep 3000

or

ps aux | grep -i rails

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.