`
peryt
  • 浏览: 54398 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

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

 

 

分享到:
评论

相关推荐

    routes源代码

    Routes is a Python re-implementation of the Rails routes system for mapping URLs to application actions, and conversely to generate URLs. Routes makes it easy to create pretty and concise URLs that ...

    Laravel开发-laravel-routes

    本文将深入探讨Laravel的路由系统,特别是`laravel-routes`这个主题,以及如何进行路由分组。 首先,我们来了解Laravel路由的基本概念。在Laravel中,路由定义在`routes/web.php`和`routes/api.php`两个文件中。...

    Laravel开发-laravel-admin-routes

    在本文中,我们将深入探讨Laravel开发中的一个重要概念——`laravel-admin-routes`,它是由Bytenet创建的一个管理路由包。这个包的主要目的是为了简化Laravel应用中后台管理界面的路由设置,使得开发者可以更高效、...

    前端开源库-react-routes

    前端开源库-react-routesReact Routes,轻量级同构HTML5路由器,用于ReactJS。

    Laravel开发-laravel-jwt-routes

    `laravel-jwt-routes` 是一个专为Laravel设计的包,它简化了使用JWT进行API认证的过程。 ### 1. JWT简介 JWT是一种轻量级的身份验证和授权机制,通过在客户端和服务器之间传递加密的令牌来确保安全的数据交换。JWT...

    python3-routes-2.4.1-12.el8.noarch.rpm

    离线安装包,亲测可用

    express-mount-routes:一个快递包,可从文件系统自动加载路由

    在yarn add express-mount-routes或npm install express-mount-routes --save : const path = require ( 'path' ) ; const express = require ( 'express' ) ; const routes = require ( 'express-mount-routes' )...

    next-routes, Next.js的通用动态路由.zip

    next-routes, Next.js的通用动态路由 Next.js 动态路由 易于使用通用动态路由的Next.js表达式和参数匹配面向 express &的请求处理程序中间件通过路由定义生成url的Link 和 Router:如何使用安装:npm

    routes-2.6.7-10.diff.txt

    从给定的文件信息来看,我们正在探讨的是Linux内核版本2.6.7到2.6.7-10之间的代码变更,具体涉及到网络过滤、路由选择以及网络流处理等核心网络功能的修改。下面将对这些变更进行详细的解析。 ...

    lanl_routes.edgelist

    Routes to LANL from 186 sites on the Internet.从186个互联网站点到LANL的路线

    Laravel开发-json-routes

    但是,通过`json-routes`,我们可以创建一个名为`routes.json`的文件,里面包含以下内容: ```json { "routes": [ { "method": "GET", "uri": "/users", "action": "App\\...

    angular2-website-routes, 使用路由的简单 Angular 2网站示例.zip

    angular2-website-routes, 使用路由的简单 Angular 2网站示例 使用路由构建 Angular 2网站这是一个使用全新路由器的简单 Angular 2网站。 它演示了如何构建组件。配置路由。注入服务,以及使用 @Input decorator将...

    Fullstack Vue: The Complete Guide to Vue.js

    Vuex-based Routes and Authentication: Build on top of the server persisted shopping cart app by creating dynamic routes and a token authentication flow with the official vue-router library. ...

    python-routes-2.4.1-1.el7.noarch.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

    Laravel开发-laravel-lock-routes

    因此,你可能需要手动将此目录添加到`composer.json`的`repositories`部分,并使用`composer require path/to/your/repository`来安装。 2. **配置**:安装后,扩展通常会提供一个配置文件,让你定义权限规则。你...

    Laravel开发-extract-routes

    "Laravel开发-extract-routes"是一个工具,由Casa Parks创建,用于帮助开发者更方便地管理和分析他们的路由配置。这个工具被称为“Laravel路线采掘机”,它提供了一种有效的方式来提取、理解和优化Laravel应用中的...

    Laravel开发-laravel-js-routes

    在现代Web开发中,前端与后端的交互变得越来越紧密,JavaScript路由(JS Routes)成为了一个关键的组件,它允许前端应用更好地理解和处理后端的路由结构。在Laravel框架中,`laravel-js-routes`是一个非常有用的库,...

    routes.js

    routes.js

Global site tag (gtag.js) - Google Analytics