Rails – simple problems
NOTE – I’m simply writing about what I find as I explore Ruby on Rails (ROR) – your mileage should vary but chances are you landed here due to a long tail search. Since I am not a Ruby or ROR expert it is quite possible for me to discover a solution or approach that works for me but which may not be a good Ruby/ROR solution. Caveat Emptor!
Rails Routes
- routes connect resources via controllers — :controller/:action/:id
- if you have a static resource (i.e. a ‘view’) that does not have a controller you may have routing issues unless you use regular routing (explicitly defined routes)
- Rails uses restful routing (via CRUD) so sometimes you have to create explicit connections - a detailed discussion on Rails routing is found here.
- Rails routes use FEUF (first encountered = use first – so place explicit routes at the top of the file with any catch-alls or defaults at the bottom)
Duplicating the problem – Steps:
- rails myproject
- cd myproject
- script/generate controller Site help about index
- rm public/index.html
- script/server
- surf to the default URL for your Rails project (i.e. http://localhost:3000/)
Note that I am using Aptana RadRails which contains features where I ran the ‘generate’ command from within the very nice GUI (lots of features - great tool.) Using RadRails I also start/stop the server and there is even a built-in browser view.
Rails Error Message:
Unknown action
No action responded to show. Actions: about, help, and index
In this case both the ‘about’ and ‘help’ Actions were not located, i.e. access the URL http://localhost:3003/site/help results in the error above. They are only present as views (no controllers) but they are part of the ’site’ (there is a site controller and there are views for about and help under the ’site’ views folder.)
When the ’site’ resource was created the entry in routes.rb was:
map.resources :site
which did not work. To get these resources working the route was changed to:
map.root :controller => “site” (default route) OR
map.connect ”, :controller => “site”, :action => “index” (empty route)
and now all works as expected. Note that this change sets the default route (the root route) the the entire Rails site. Note that a default route “makes every action of every controller in your application accessible to GET requests” - which may NOT be what you want…
An interesting side effect – if I add a route for ‘help’ then ‘about’ begins working as well – hmm.
map.help ‘help’, :controller => ’site’, :action => ‘help’
and the URL becomes: http://localhost:3003/help while about only works with: http://localhost:3003/site/about and ‘/’ no longer works…
A bit more – you can glean a bit more by using the command rake routes OR by running the Rails Console – script/console – which starts up an instance of IRB.
>> my_routes = ActionController::Routing::Routes (lots of data scrolls by after you press RETURN)
and then request a listing of routes using puts (output will vary) – in this case I have one controller (site) and my defined route (from routes.rb) includes map.resources :site as well as the two defaults at the end of the file:
>> puts my_routes.routes
GET /site(.:format)? {:controller=>"site", :action=>"index"}
POST /site(.:format)? {:controller=>"site", :action=>"create"}
GET /site/new(.:format)? {:controller=>"site", :action=>"new"}
GET /site/:id/edit(.:format)? {:controller=>"site", :action=>"edit"}
GET /site/:id(.:format)? {:controller=>"site", :action=>"show"}
PUT /site/:id(.:format)? {:controller=>"site", :action=>"update"}
DELETE /site/:id(.:format)? {:controller=>"site", :action=>"destroy"}
ANY /:controller/:action/:id/ {}
ANY /:con
When I change to the map.root route then the first line becomes:
ANY / {:controller=>"site", :action=>"index"}
and everything else is the same. If I use the named route for ‘help’ then the first line of my routes becomes:
ANY /help/ {:controller=>"site", :action=>"help"}
Related posts:
- Rails – where is the missing stuff? ROR (Ruby on Rails) is advertised as a data-centric development...
- CMS Made Simple – 1st impressions Made Simple? Let's see how long it takes before the...
- Fedora 10 install problems Could just be me… Could just be luck of the...
- Zena – Rails CMS & VDI Playground CMS=Content Management System Rails = Ruby on Rails – a...
- Blog software: First set of problems… Site performance is Ok, but seems a bit slow...