如何设置和使用Rails路由前缀

时间:2014-07-08 02:33:19

标签: ruby-on-rails ruby-on-rails-4

在运行rake路线时,我看到:

 Prefix   Verb   URI Pattern                                       Controller#Action
 articles  GET    /articles(.:format)                               articles#index
 new_article GET    /articles/new(.:format)                           articles#new

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

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

resources :articles

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

根据我的理解,它是URI的快捷方式,所以在我的应用程序中我可以引用new_article而不是articles / new?我真的参考@new_article还是有其他格式来引用它。

第二个问题,我该怎么编辑?假设我想要一个名为new_document的前缀而不是new_article用于该URL上的get请求。我会在routes.rb文件中添加什么?

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.

7 个答案:

答案 0 :(得分:18)

<强>路径

我不确定为什么称它为prefix - 它应该被称为path helper

enter image description here

底线是当您调用link_toform_tag等帮助程序时 - 他们需要paths来填充应用程序路由结构中的不同操作。

由于Rails赞成convention over configuration&amp; DRY programming,这意味着如果您可以使用标准path helpers引用这些urls,则可以使用一个参考&amp;根据需要获得路线

EG

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


<强>路线

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

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

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

要回答您的问题,您需要了解您应始终按原样引用资源(在您的情况下为articles)。

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

#config/routes.rb
resources :articles, as: :document, path: "document" #-> domain.com/documents

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

答案 1 :(得分:15)

您可以在控制器和视图中使用new_article_pathnew_article_url等快捷方式。在执行将用户重定向到特定页面或生成动态链接等操作时,这些功能非常方便。

您可以通过在routes.rb文件中添加as:选项来更改前缀。例如:

GET '/new', to: 'articles#new', as: 'my_new_article`

这会将前缀更改为my_new_article

答案 2 :(得分:6)

你说得对,前缀用于构建命名路由,因此路由文件中的resources method创建了articles_path, new_article_path等路由。要在代码中引用路由,将path添加到running rake routes时看到的前缀。

如果您想更改路线文件中资源方法生成的前缀,请使用 :as option ,如下所示:

<强> resources :articles, as: :posts

这会产生类似&#39; new_post_path&#39;的路线。您可以在视图中使用。

如果要在浏览器中更改路径的路径,可以使用path选项:

resources :articles, path: :posts

这将生成/posts/1 or /posts/new而非/articles/1 or /articles/new之类的路由,但您的代码中的路由仍将被命名为articles_path, new_article_path等。您可以使用两个:path and :as选项更改资源路由的path and the prefix

要更改路线文件中的基本路线,您只需使用:as,就像这样:

<强> get 'messages' => 'messages#index', as: :inbox

这将创建您可以在代码中使用的路径inbox_path。

希望有所帮助!

答案 3 :(得分:2)

您也可以使用route scopes。在您的routes.rb文件中:

Rails.application.routes.draw do

  # Any other routes here
  scope 'admin' do
    resources :articles
  end
end

/admin/articles和所有其他与CRUD相关的路由将与link_to,表单提交控件等一起使用。

答案 4 :(得分:1)

前缀可与路径助手一起使用,以在您的应用程序中生成路由。因此,在您的应用程序中,articles_path帮助程序为文章索引页面生成路径,new_article_path是新文章页面的路径,article_path(@article)将是@article的显示页面的路径。

要指定其他路线,您可以将routes.rb文件更改为:

resources :articles, as: :documents

那会给你:

documents  GET    /articles(.:format)                               articles#index
new_document GET    /articles/new(.:format)                           articles#new

我可能会考虑将资源名称更改为文档,因为当您重命名路由并在路由和控制器之间混合使用术语时,它会变得混乱。

答案 5 :(得分:1)

您可以这样做:

resources :articles do
 collection do
      get  "new"  => "articles#new",  :as => 'new_document'
 end
end

你可以使用它:

link_to "New foo", new_document_path 

link_to "New foo", new_document_url

url打印绝对网址(http://server.com/controller/action),同时路径打印相对网址(/controller/action)。

答案 6 :(得分:0)

您可以尝试config.ru文件

 map 'YOUR_PREFIX_HERE' do
   run Rails.application
 end