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.
A route announcement is sometimes referred to as a 'prefix'. A prefix is composed of a path of AS numbers, indicating which networks the packet must pass through, and the IP block that is being routed, so a BGP prefix would look something like: 701 1239 42 206.24.
Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With