【问题标题】:how to set up and use a Rails routes prefix如何设置和使用 Rails 路由前缀
【发布时间】:2014-08-28 15:47:00
【问题描述】:

在运行 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.

【问题讨论】:

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


    【解决方案1】:

    路径

    我不知道为什么叫prefix——应该叫path helper

    最重要的是,当您调用 link_toform_tag 等助手时 - 他们将需要 paths 来填充应用路由结构中的不同操作。

    由于 Rails 偏爱convention over configurationDRY programming,这意味着如果您可以参考这些path helpers 而不是使用标准urls,它将允许您根据需要进行一次参考和机会

    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
    

    这允许您定义自定义路由/路径助手,允许您随意调用它们

    【讨论】:

      【解决方案2】:

      你可以在config.ru文件里试试

       map 'YOUR_PREFIX_HERE' do
         run Rails.application
       end
      

      【讨论】:

      • 这不处理资产,至少在开发中
      【解决方案3】:

      您也可以使用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】:

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

        如果你想在你的路由文件中改变resources方法生成的前缀,使用:as option,像这样:

        resources :articles, as: :posts

        这将生成诸如“new_post_path”之类的路由,您可以在视图中使用它们。

        如果要在浏览器中更改路由的路径,可以使用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。

        希望有帮助!

        【讨论】:

          【解决方案5】:

          你可以这样做:

          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 打印绝对 url (http://server.com/controller/action) 同时 path 打印相对 url (/controller/action)。

          【讨论】:

            【解决方案6】:

            前缀可以与路由助手一起使用,以在您的应用程序中生成路由。因此,在您的应用程序中,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
            

            我可能会考虑将资源名称更改为文档,因为当您重命名路由并在路由和控制器之间获得混合术语时,它会让人感到困惑。

            【讨论】:

              【解决方案7】:

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

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

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

              这会将前缀更改为my_new_article

              【讨论】:

                猜你喜欢
                • 2015-01-10
                • 2019-11-13
                • 2010-10-25
                • 2020-05-17
                • 2020-10-27
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多