Help understanding a very simple routes.db

Hi All,

I am following a Ruby on rails tutorial and everything is working but I am struggling a bit to follow what is happening and why:

I have created the following routes.db and I just want some help understanding what it is doing?

SampleApp::Application.routes.draw do   get "pages/home"   get "pages/contact"   get "pages/about"   get "pages/help"

  match '/contact', :to => 'pages#contact'   match '/about', :to => 'pages#about'   match '/help', :to => 'pages#help'

  root :to => 'pages#home' end

I am in the process of reading Rails Routing from the Outside In — Ruby on Rails Guides but it is a learning curve!

Is it saying to match '/contact' with 'pages/contact' and to get 'pages/contact'? If so what is the # for in 'pages#contact'?

Any help understanding really appreciated!

Thanks

Matt

match '/contact', :to => 'pages#contact'

reads, match path '/contact' and load controller Pages and execute action 'contact'

however you can remove the comma and also ':to =>' to define the route as:

  match '/contact' => 'pages#contact'

if you run 'rake routes' you will see:

contact /contact(.:format) {:controller=>"pages", :action=>"contact"}

this tells you that the format "pages#contact" is in the form:

  controller#action

Ok - Thanks so I was reading it wrong! but then all that is in the pages_controller.rb is:

class PagesController < ApplicationController

  def home     @title = "Home"   end

  def contact     @title = "Contact"   end

  def about     @title = "About"   end

  def help     @title = "Help"   end end

Doesn't this just define some variables? what is the action?

i assume you're reading the "ruby on rails tutorial"?, you need to understand rails MVC conventions, i suggest you reread the early chapters =)

when a request comes in the controller is loaded and an action is run, the controller can then request the model to fetch some data, the class instance variables defined (those defined with @) in the controller are accessible from the view. rails display the view following a controller action.

request -> controller -> action -> model(optional) -> view

the view will have the same name as the action.

if you browser to 'http://ocalhost:3000/pages/home’ then the Pages controller will be loaded and it will call the home action, which will assign a variable and then display view home.html.erb

the action are defined as ruby method using 'def/end' blocks: here the defined actions are obviously: home, contact, about and help

Thanks I am trying to keep re-reading it till it sinks in but can I ask then:

def contact     @title = "Contact"   end

If this creates 'contact' as the action (I understand the instance variable of @title which can be seen in the view) where is it defining what that action does or is it simply mapping to the name 'contact.html.erb'?

In which case how does it know where to find 'contact.html.erb' is it looping back to the 'routes.db' and checking the "get pages/contact"?

Your help is really appreciated and I hope that I am close to getting it...

yes each action assigns a value to a variable, you'll see other examples where real actions are defined later on! one step at a time =)

rails follows convention over configuration, read my last email and go over MVC in the tutorial, page 1

http://railstutorial.org/chapters/beginning#sec:mvc

http://railstutorial.org/chapters/a-demo-app#sec:mvc_in_action

basically rails know what view to call based on the action by using the same name as the action! this is the rails convention (way of doing things!).