routes, add links to views

this chapter is very simple.

 

in previous chapters, we just use "#" as the url in the links.

 

now we will fill with actual link urls

 

1. we can hard code to:

<a href="pages/about">About</a>

 

but it works, but this is not rails way

 

a. it will disclose "pages" controller, if the url is "/about" will be better.

b. rails like to use named routes.

 

so in rails way, it should be:

<%= link_to "About", about_path %>

 

in this way, it will be more flexible, because if about_path is used in many places, we just need to modify the about_path in route file alone.

 

 

2. in our routes file, the code is like:

 

SampleApp::Application.routes.draw do
  match "/contact", :to => 'pages#contact'
  match "/about", :to => 'pages#about'
  match '/help', :to => 'pages#help'
end

 

(note, after adding these routes, you needn't add

get 'pages/contanct', because

this url already visible by rails.)

 

these code are well self-explained, match a route '/contact', to the 'contact' action in pages controller.

 

what is not obvious is that,

 

match '/about' will also create a named routes behind the curtain.

when you define this, you can already use 

about_path   => '/about'

about_url    => 'http://yourdomain:3000/about', this is the full url.

 

the differences between about_path and about_url rarely matters in practice.

But I like using about_path everywhere.

 

ok, next, we need to implement homepage route.

you can use

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

but this is unnecessary, you should use

 

root :to => 'pages#home'

(the comments in routes.rb file already include this, you can just uncomment it to make it work.)

 

This will also create two named routes:

root_path,   => '/'

root_url     ===>  'http://yourdomain:3000/'

(you need to delete the public/index.html to make the new home page work.)

 

then you need to remove the default home page file public/index.html

 

git rm public/index.html

git commit -am "Removed default homepage file"

 

(note, here we rolled the two flag in git commit into one

 

git commit -a -m "message"  
is equal to
git commit -am "message")

 

 

3. now, we have the routes, next, we need to fill in links into views:

 

first, we fill in the about path:

 

<%= link_to 'About', about_path %>

 then we try to add a link to the logo, so that is will lead to home page:

<% logo = image_tag('logo.png', :alt => "logo", :class => "round") %>
<%= link_to logo, root_path %>

 this way, we defined a local variable logo, then use it, this is better than insert all things into one line.

 

another cleaner way is to use helper, (helper is used to have method for use in views, not controllers, the conroller's helper methods will be auto visible by the views of that controller.)

because this way will make view more concise!!!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值