Routes & methods in controllers


#1

still a newbie in rails so bear with me on this one

new test app… rails test
generate controller main

i add a layout view for main/index & its working ok
i set the routes as follows
map.connect ‘’, :controller => “Main”

the above is the only route thats active

in the MainController i add in a new method “about”
with a simple render_text “hello” in it

refresh the page & i get this error
No url can be generated for the hash {:action=>“about”,
:controller=>“Main”}

I thought that since the default route is now going to main, shouldnt
http://0.0.0.0:3000/about now show me the about page ?

or is there something else i should be doing in my routes ?

thanks,
pete


#2

I believe you want something like this for your routes:

map.connect ‘:action’, :controller => ‘Main’

This way when it matches /about with this rule, it will set the symbol
:action to ‘about’. That’s why the default:

map.connect ‘:controller/:action/:id’

works without explicitly setting :controller, :action, or :id to
anything.

HTH, Ryan

View this message in context:
http://www.nabble.com/routes---methods-in-controllers-t1764675.html#a4803395
Sent from the RubyOnRails Users forum at Nabble.com.


#3

Hi Ryan,
Thanks for the quick reply.
That did work, but if i add in any other controllers .eg foo
then any action associated with foo will not show up
So does this mean i have to add in every controller that I create into
routes
or is there one generic way.

Pete

Wuher wrote:

I believe you want something like this for your routes:

map.connect ‘:action’, :controller => ‘Main’

This way when it matches /about with this rule, it will set the symbol
:action to ‘about’. That’s why the default:

map.connect ‘:controller/:action/:id’

works without explicitly setting :controller, :action, or :id to
anything.

HTH, Ryan

View this message in context:
http://www.nabble.com/routes---methods-in-controllers-t1764675.html#a4803395
Sent from the RubyOnRails Users forum at Nabble.com.


#4

Hey Pete,
Again, I’m not an expert, but this is the impression I’m under:

Your routes file is simply a list of rules that Rails tries to match
with
the request url. It starts on the top and stops at the first rule it
matches. So you want to have your most general rule at the bottom.
Gernerally, the rule:

‘:controller/:action/:id’ is good to have at the bottom, catching pretty
much all the other controllers that you haven’t explicitly defined. So
in
that case, if you define the controller foo and go to /foo, it will
catch
it, setting :controller => ‘foo’, :action => ‘index’ and :id => nil.
Or,
/foo/bar would map :controler => ‘foo’, :action => ‘bar’, and :id =>
nil.

I found the wiki page on routes helpful:
http://wiki.rubyonrails.com/rails/pages/Routes
Also (found on that page) I like this fairly short guide:
http://manuals.rubyonrails.com/read/book/9

HTH, Ryan

View this message in context:
http://www.nabble.com/routes---methods-in-controllers-t1764675.html#a4809479
Sent from the RubyOnRails Users forum at Nabble.com.