Forum: Ruby on Rails routes & methods in controllers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F6c8926493ae96b4ed5abb561bcca212?d=identicon&s=25 Pete Gajria (petegajria)
on 2006-06-10 01:56
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
2c3e9116fabc3e9f09927d1ffa8bf9e3?d=identicon&s=25 Wuher (Guest)
on 2006-06-10 02:12
(Received via mailing list)
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-controll...
Sent from the RubyOnRails Users forum at Nabble.com.
F6c8926493ae96b4ed5abb561bcca212?d=identicon&s=25 Pete Gajria (petegajria)
on 2006-06-10 16:31
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-controll...
> Sent from the RubyOnRails Users forum at Nabble.com.
2c3e9116fabc3e9f09927d1ffa8bf9e3?d=identicon&s=25 Wuher (Guest)
on 2006-06-10 19:08
(Received via mailing list)
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-controll...
Sent from the RubyOnRails Users forum at Nabble.com.
This topic is locked and can not be replied to.