I bought Agile Web Development with Rails over the weekend and I am in love. I can't believe how powerful Rails is and how much fun it makes coding! Anyway, on to my question.
Assume I have a project here:
I understand that I will find the default public view in:
I've deleted this file and have done the following in C:\rails\project\config\routes.rb:
map.connect '', :controller => "index"
My index controller is sitting in:
and my index.rhtml view in:
Booting webrick form C:\rails\project and browsing to http://localhost:3000/ does indeed bring up my index.rhtml view. The problem is that within index.rhtml I have done the following:
<%= link_to "Visit the store!", :action => "store" %><br />
<%= link_to "Admin", :action => "admin" %>
... which point to http://localhost:3000/index/store and http://localhost:3000/index/admin rather than http://localhost:3000/store and http://localhost:3000/admin.
My question, then, is how do I create a default index controller/view that would be used as, or act as, a typical index.php file in the web docroot of your website? Am I on the right path and just missing something?
The default rails route will always route /controller/action to Controller::Action - if you want to route /action to SomeDefaultController::Action you'll need to setup routes.
But, it sounds like your admin and store actions really want to be controllers of their own.
Thanks for the reply Luke. I think half of my problem is the way I asked the question. What i was asking is say you have a site ... http://www.example.com. How do you integrate your controllers into your homepage (example.com/index.rhtml)? This is what I came up with:
In config/routes.rb I changed the default route to:
and then in the index controller i did this:
# use the index controller as the default
map.connect ':controller/:action/:id', :controller => 'index'
And these links will point to something like:
<%= link_to "Visit the store!", :controller => "store", :action => "index" %><br />
<%= link_to "Admin", :controller => "admin", :action => "index" %>
... rather than
Well in your first example you only specified the action which is why it took the current controller (index) to be the one you wanted and linked to index/store and index/admin. In your above example you have actually specified store and admin as controllers with the action being index so they should be linking to store/index and admin/index.
What is your "index" controller for anyway - as a rule, controller names should imply some kind of role but "index" doesn't really say much.