如何设置和使用 Rails 路由前缀 [英] how to set up and use a Rails routes prefix

查看:24
本文介绍了如何设置和使用 Rails 路由前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行 rake 路线时,我看到:

 前缀动词 URI 模式控制器#Action文章 GET/articles(.:format) 文章#indexnew_article GET/articles/new(.:format) 文章#new

以及我遗漏的其他一些人.

这是因为我的 routes.rb 文件有:

资源:文章

我的问题是这个前缀到底是什么,我将如何更改它?

据我所知,它是 URI 的快捷方式,因此在我的应用程序中,我可以只引用 new_article 而不是文章/new?我是真的引用@new_article 还是有其他格式可以引用它.

第二个问题,我该如何编辑?假设我想要一个名为 new_document 而不是 new_article 的前缀用于该 url 上的获取请求.我会在我的 routes.rb 文件中放什么?

&干编程,意思是如果你可以参考这些路径助手使用标准的 urls,它会让你做一个参考 &根据需要选择路线

EG

调用articles_path比每次都引用/articles强大

<小时>

路线

要正确回答您的问题,您需要了解 Rails 使用 resourceful 路由 - 基本上意味着您创建的每个 route helper 应该围绕应用程序中的任何资源进行定义

由于 Rails 的 MVC 结构,这些资源通常由您使用的 controllers 定义:

#config/routes.rb资源:文章#->文章路径等

您应该始终按原样引用您的资源(在您的情况下为文章).

要自定义路径助手,您需要更改路由文件中的引用,如下所示:

#config/routes.rb资源:文章,如::文档,路径:文档"#->domain.com/documents

这允许您定义自定义路由/路径助手,允许您根据需要调用它们

When running rake routes I see:

 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:

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.

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?

I looked at this link from the rails guides but all it seems to show is how to make the url longer by adding a prefix in front of it.

解决方案

Paths

I'm not sure why it's called prefix - it should be called path helper:

The bottom line is when you call helpers like link_to or form_tag etc - they will require paths to populate different actions in your app's routing structure.

As Rails favours convention over configuration & DRY programming, meaning if you can reference these path helpers over using standard urls, it will allow you to make one reference & chance the route as required

EG

Calling articles_path is far more powerful than referencing /articles every time


Routes

To answer your question properly, you will need to appreciate Rails uses resourceful routing - basically meaning that every route helper you create should be defined around any resource in your application

Due to the MVC structure of Rails, these resources will typically be defined by the controllers you use:

#config/routes.rb
resources :articles #-> articles_path etc

You should always reference your resources as they are (in your case articles).

To customize the path helper, you'll need to change the 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

这篇关于如何设置和使用 Rails 路由前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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