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!!!
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 824the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8581. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9131. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8791.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7551. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8991. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 7031. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11071. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 744in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 774in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 665again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7091. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 737whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7501. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 727There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 658start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 645a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 721in integration test, you can te ...
相关推荐
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/web.php`和`routes/api.php`两个文件中。...
在本文中,我们将深入探讨Laravel开发中的一个重要概念——`laravel-admin-routes`,它是由Bytenet创建的一个管理路由包。这个包的主要目的是为了简化Laravel应用中后台管理界面的路由设置,使得开发者可以更高效、...
前端开源库-react-routesReact Routes,轻量级同构HTML5路由器,用于ReactJS。
`laravel-jwt-routes` 是一个专为Laravel设计的包,它简化了使用JWT进行API认证的过程。 ### 1. JWT简介 JWT是一种轻量级的身份验证和授权机制,通过在客户端和服务器之间传递加密的令牌来确保安全的数据交换。JWT...
离线安装包,亲测可用
在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的通用动态路由 Next.js 动态路由 易于使用通用动态路由的Next.js表达式和参数匹配面向 express &的请求处理程序中间件通过路由定义生成url的Link 和 Router:如何使用安装:npm
从给定的文件信息来看,我们正在探讨的是Linux内核版本2.6.7到2.6.7-10之间的代码变更,具体涉及到网络过滤、路由选择以及网络流处理等核心网络功能的修改。下面将对这些变更进行详细的解析。 ...
Routes to LANL from 186 sites on the Internet.从186个互联网站点到LANL的路线
但是,通过`json-routes`,我们可以创建一个名为`routes.json`的文件,里面包含以下内容: ```json { "routes": [ { "method": "GET", "uri": "/users", "action": "App\\...
angular2-website-routes, 使用路由的简单 Angular 2网站示例 使用路由构建 Angular 2网站这是一个使用全新路由器的简单 Angular 2网站。 它演示了如何构建组件。配置路由。注入服务,以及使用 @Input decorator将...
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. ...
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
因此,你可能需要手动将此目录添加到`composer.json`的`repositories`部分,并使用`composer require path/to/your/repository`来安装。 2. **配置**:安装后,扩展通常会提供一个配置文件,让你定义权限规则。你...
"Laravel开发-extract-routes"是一个工具,由Casa Parks创建,用于帮助开发者更方便地管理和分析他们的路由配置。这个工具被称为“Laravel路线采掘机”,它提供了一种有效的方式来提取、理解和优化Laravel应用中的...
在现代Web开发中,前端与后端的交互变得越来越紧密,JavaScript路由(JS Routes)成为了一个关键的组件,它允许前端应用更好地理解和处理后端的路由结构。在Laravel框架中,`laravel-js-routes`是一个非常有用的库,...
routes.js