Ruby-on-rails 如何设置和使用 Rails 路由前缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24622826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set up and use a Rails routes prefix
提问by Frank Visaggio
When running rake routes I see:
在运行 rake 路线时,我看到:
Prefix Verb URI Pattern Controller#Action
articles GET /articles(.:format) articles#index
new_article GET /articles/new(.:format) articles#new
along with some others I left out.
以及我遗漏的其他一些人。
This is because my routes.rb file has:
这是因为我的 routes.rb 文件有:
resources :articles
My question is what exactly is this Prefix, and how would I change it?
我的问题是这个前缀到底是什么,我将如何更改它?
From my understanding it is a shortcut to the URI, so in my app I could just refer to new_article instead of articles/new? Do I really refer to @new_article or is there some other format to reference it.
根据我的理解,它是 URI 的快捷方式,所以在我的应用程序中,我可以只引用 new_article 而不是 article/new? 我是真的引用@new_article 还是有其他格式可以引用它。
Second question, how do I edit that? Lets say I want a prefix called new_document instead of new_article for the get request on that url. What would I put in my routes.rb file?
第二个问题,如何编辑?假设我想要一个名为 new_document 而不是 new_article 的前缀用于该 url 上的获取请求。我会在我的 routes.rb 文件中放什么?
回答by Ege Ersoz
It let's you use shortcuts such as new_article_pathor new_article_urlin your controllers and views. These come in very handy when doing things like redirecting users to a specific page or generating dynamic links.
它让您可以在控制器和视图中使用快捷方式,例如new_article_path或new_article_url。这些在执行诸如将用户重定向到特定页面或生成动态链接之类的操作时非常方便。
You can change the prefix by adding the as:option in your routes.rb file. For example:
您可以通过as:在 routes.rb 文件中添加选项来更改前缀。例如:
GET '/new', to: 'articles#new', as: 'my_new_article`
This would change the prefix to my_new_article.
这会将前缀更改为my_new_article.
回答by Richard Peck
Paths
路径
I'm not sure why it's called prefix- it should be called path helper:
我不确定为什么叫它prefix- 应该叫它path helper:


The bottom line is when you call helpers like link_toor form_tagetc - they will require pathsto populate different actions in your app's routing structure.
最重要的是,当您调用诸如link_to或form_tag等之类的助手时- 他们将需要paths在您的应用程序的路由结构中填充不同的操作。
As Rails favours convention over configuration& DRY programming, meaning if you can reference these path helpersover using standard urls, it will allow you to make one reference & chance the route as required
由于 Rails 偏爱convention over configuration& DRY 编程,这意味着如果您可以参考这些而path helpers不是使用标准urls,它将允许您进行一次参考并根据需要选择路线
EG
例如
Calling articles_pathis farmore powerful than referencing /articlesevery time
电话articles_path是远远比引用更强大的/articles每次
Routes
路线
To answer your question properly, you will need to appreciate Rails uses resourcefulrouting- basically meaning that every routehelper you create shouldbe defined around any resource in your application
要正确回答您的问题,您需要了解 Rails 使用resourceful路由- 基本上意味着route您创建的每个助手都应该围绕您的应用程序中的任何资源进行定义
Due to the MVC structure of Rails, these resources will typically be defined by the controllersyou use:
由于 Rails 的 MVC 结构,这些资源通常由controllers您使用的定义:
#config/routes.rb
resources :articles #-> articles_path etc
You should always reference your resources as they are (in your case articles).
您应该始终按原样引用您的资源(就您而言articles)。
To customize the path helper, you'll need to changethe reference in the routes file, like this:
要自定义路径助手,您需要更改路由文件中的引用,如下所示:
#config/routes.rb
resources :articles, as: :document, path: "document" #-> domain.com/documents
This allows you to define custom routes / path helpers, allowing you to call those as you wish
这允许您定义自定义路由/路径助手,允许您根据需要调用它们
回答by user3376757
You're right, the prefix is used in building the named routes, so the resources methodin the routes file creates routes like articles_path, new_article_pathetc. To refer to a route in your code, add pathto the prefix that you see when running rake routes.
您说得对,前缀用于构建命名路由,因此resources method路由文件中的 会创建类似articles_path, new_article_path等的路由。要在代码中引用路由,请添加path您在 when 看到的前缀running rake routes。
If you want to change the prefixes generated by the resources method in your routes file, use the :as option, like this:
如果要更改路由文件中资源方法生成的前缀,请使用:as option,如下所示:
resources :articles, as: :posts
resources :articles, as: :posts
This will generate routes like 'new_post_path' that you can use in your views.
这将生成您可以在视图中使用的诸如“new_post_path”之类的路由。
If you want to change the path of the route in the browser, you can use the pathoption:
如果要在浏览器中更改路由的路径,可以使用该path选项:
resources :articles, path: :posts
Which will generate routes like /posts/1 or /posts/newinstead of /articles/1 or /articles/new, but the routes will still be named articles_path, new_article_pathetc in your code. You can use both :path and :asoptions to change both the path and the prefixof your resource routes.
这将生成类似于/posts/1 or /posts/new而不是/articles/1 or /articles/new的路由,但这些路由仍将articles_path, new_article_path在您的代码中命名为etc。您可以使用这两个:path and :as选项来更改path and the prefix您的两个资源路由。
To change a basic route in your routes file, you can simply use :as, like so:
要更改路由文件中的基本路由,您可以简单地使用:as,如下所示:
get 'messages' => 'messages#index', as: :inbox
get 'messages' => 'messages#index', as: :inbox
This will create the route inbox_path which you can use in your code.
这将创建您可以在代码中使用的路由 inbox_path。
Hope that helps!
希望有帮助!
回答by Leonel Sanches da Silva
You can also use route scopesfor it. In your routes.rbfile:
您还可以为其使用路由范围。在您的routes.rb文件中:
Rails.application.routes.draw do
# Any other routes here
scope 'admin' do
resources :articles
end
end
/admin/articlesand all the other CRUD related routes will work with link_to, form submit controls, etc.
/admin/articles并且所有其他与 CRUD 相关的路由都可以使用link_to,表单提交控件等。
回答by mdenomy
The prefix can be used with the route helpers to generate routes in your application. So in your application, the articles_path helper generates a route for articles index page, new_article_path is the route for the new article page, and article_path(@article) would be the route for the show page for @article.
前缀可以与路由助手一起使用以在您的应用程序中生成路由。因此,在您的应用程序中,articles_path 助手生成文章索引页面的路由,new_article_path 是新文章页面的路由,而 article_path(@article) 将是@article 显示页面的路由。
To specify a different route, you could change your routes.rb file to:
要指定不同的路由,您可以将 routes.rb 文件更改为:
resources :articles, as: :documents
That will give you:
这会给你:
documents GET /articles(.:format) articles#index
new_document GET /articles/new(.:format) articles#new
I might consider changing the resource name to documents as it can get confusing when you rename routes and get a mix of terminology between the route and the controller.
我可能会考虑将资源名称更改为文档,因为当您重命名路由并在路由和控制器之间混合使用术语时,它会引起混淆。
回答by aarkerio
You can do something like this:
你可以这样做:
resources :articles do
collection do
get "new" => "articles#new", :as => 'new_document'
end
end
and you can use it:
你可以使用它:
link_to "New foo", new_document_path
or
或者
link_to "New foo", new_document_url
url prints the absolute url (http://server.com/controller/action) meanwhile path prints the relative url (/controller/action).
url 打印绝对 url ( http://server.com/controller/action) 同时 path 打印相对 url ( /controller/action)。
回答by Amit Agarwal
You can try in config.ru file
您可以在 config.ru 文件中尝试
map 'YOUR_PREFIX_HERE' do
run Rails.application
end

