且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Rails 中更改 URL

更新时间:2022-04-06 01:29:57

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

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