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
http://guides.rubyonrails.org/routing.html
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

On 10-10-19 05:20 PM, Murph2m wrote:

get “pages/contact”
I am in the process of reading http://guides.rubyonrails.org/routing.html

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


Kind Regards,
Rajinder Y. | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1

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?

On 10-10-20 05:18 AM, Murph2m wrote:

 @title = "Contact"

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


Kind Regards,
Rajinder Y. | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1

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…

On 10-10-20 06:13 AM, Rajinder Y. wrote:

def contact
end

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

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


Kind Regards,
Rajinder Y. | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1

On 10-10-20 06:33 AM, Murph2m wrote:

‘contact.html.erb’?

@title = “Home”
def help
@title = “Help”
end
end

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

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

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!).


Kind Regards,
Rajinder Y. | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs