how to set up and use a Rails routes prefix

后端 未结 7 909
北海茫月
北海茫月 2021-02-05 07:39

When running rake routes I see:

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


        
相关标签:
7条回答
  • 2021-02-05 07:53

    Paths

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

    enter image description here

    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

    0 讨论(0)
  • 2021-02-05 08:00

    You can also use route scopes for it. In your routes.rb file:

    Rails.application.routes.draw do
    
      # Any other routes here
      scope 'admin' do
        resources :articles
      end
    end
    

    /admin/articles and all the other CRUD related routes will work with link_to, form submit controls, etc.

    0 讨论(0)
  • 2021-02-05 08:00

    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.

    To specify a different route, you could change your routes.rb file to:

    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.

    0 讨论(0)
  • 2021-02-05 08:06

    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).

    0 讨论(0)
  • 2021-02-05 08:07

    You can try in config.ru file

     map 'YOUR_PREFIX_HERE' do
       run Rails.application
     end
    
    0 讨论(0)
  • 2021-02-05 08:10

    It let's you use shortcuts such as new_article_path or new_article_url in your controllers and views. These come in very handy when doing things like redirecting users to a specific page or generating dynamic links.

    You can change the prefix by adding the as: option in your routes.rb file. For example:

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

    This would change the prefix to my_new_article.

    0 讨论(0)
提交回复
热议问题