Rails路由-资源的自定义路由 [英] Rails routing - custom routes for Resources

查看:149
本文介绍了Rails路由-资源的自定义路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个Rails应用,我想坚持使用REST和Resources之类的所有奇特的东西,但我想稍微自定义路线。我希望我的GET路线更加冗长-我正在创建的应用程序是一个简单的博客,因此,我宁愿< GET / posts / 1 code> GET / posts / 1-my-first-post 。

I'm building currently one Rails app and I'd like to stick to all those fancy things like REST and Resources, but I'd like to customise my routes a little. I want my GET route to be little more verbose - the app I'm creating is a simple blog, so instead of GET /posts/1 I'd prefer something like GET /posts/1-my-first-post.

任何想法如何做到这一点?

Any ideas how to do this? Didn't find anything on the web.

推荐答案

路由:

map.resources :posts

型号:

class Post < ActiveRecord::Base
  def to_param
    "#{id.to_s}-#{slug}"
  end
end

应该做到这一点。

Btw: http://railscasts.com/episodes/63-model-name-in-url

这篇关于Rails路由-资源的自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆