如何在 Rails 中更改 URL [英] How to change URL in Rails

查看:55
本文介绍了如何在 Rails 中更改 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Book 的资源,然后我有以下域:

I have a resource called Book, then I have domains like:

   domain.com/books/272

但是我想把它改成

   domain.com/stories/272

仅针对 URL,无需更改控制器、类等

Only for the URL, don't need to change controller, classes etc.

在我拥有的路线中

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

  map.root :controller => 'static'

我该怎么做?谢谢

推荐答案

实际上取决于您已经拥有的内容.

Depends really on what you have already.

将此代码用于您的路由文件:(如果将 books 的原始 URL 替换为 stories)

Use this code to your routes file: (in the case of the original URL of books replaced by stories)

#resource routes
map.resources :books, :as => :stories
#named routes
map.books 'stories/:id'

在不定义路由的情况下,我能想到的唯一选择 - 这看起来非常错误 - 添加一个从您的书籍控制器继承的新控制器.您需要检查您的应用程序并更改用于生成路径或 URL 的控制器名称,如下例所示:

Without defining routes the only option I can think of - which seems terribly wrong - is to add a new controller which inherits from your books controller. You'd need to go through your application and change the controller name used to generate paths or URLs as seen in the following example:

class BooksController < ApplicationController

class StoriesController < BooksController

就我个人而言,我建议您花时间定义路由,但我想这取决于您使用的应用程序有多大.

Personally, I would recommend you take the time to define your routes but I guess this depends on how large an application you're working with.

本指南将帮助您了解 RoR 中的路由:http://guides.rubyonrails.org/routing.html

This guide will help you understand routing in RoR: http://guides.rubyonrails.org/routing.html

这篇关于如何在 Rails 中更改 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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