---
title: "ROR-REST风格入门(1)"
author: "taito"
date: "2008-04-01T00:13:30Z"
tags: []
categories: ["非技术"]
slug: "ror-rest-1"
draft: false
lastmod: "2008-04-01T00:13:30Z"
id: 178241
url: "https://taito.iteye.com/blog/178241"
---

如果想用REST风格的ROR

比如访问地址如此精简

 http://www.taito.com/admin/1      <-- 自动调用 show 方法
 http://www.taito.com/admin/1/edit <-- 自动调用 edit 方法

views的代码如此精简

 原始的代码:
 link_to :controller => "admin", :action => "show", :id => project

 现在的代码:
 link_to "Show", admin_path(admin)

＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝
只需做以下几步即可，具体的意思这里就不多描述了，网上大把介绍。

  在configuration 文件夹中的routes.rb 中定义 
<span style="color:red">  map.resources :admins</span>   

  注意要放在  map.connect... 前  

  
  备注：
  在默认的情况下，controller里就必须有show, index, update, delete,create, destroy, new, edit。这都是ROR规定的习惯，所以，每一个REST的 controller 都必须实现这几个方法。

  这样你就可以用此访问如此简明的URL了。