{"id":9723,"date":"2024-03-04T12:20:27","date_gmt":"2024-03-04T12:20:27","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9723"},"modified":"2024-03-04T12:20:27","modified_gmt":"2024-03-04T12:20:27","slug":"as-in-rails-routesrb","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/as-in-rails-routesrb","title":{"rendered":":as in Rails routes.rb"},"content":{"rendered":"<p><strong>:as<\/strong> option is used to create named routes in rails. It allows you to specify a name for a route.These helpers make it easier to generate URLs and paths within your application, especially when dealing with resourceful routes.<\/p>\n<h3>Example:-<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\"># config\/routes.rb\r\nRails.application.routes.draw do\r\n  resources :posts, as: 'articles'   \r\n  # 'articles' is the custom name for the route helpers\r\nend<\/pre>\n<p>In the example above, the :as option is used to specify that the route for articles should be referred to as &#8220;<strong>articles<\/strong>&#8221; in the named route helpers. This means that instead of using<strong> post_path(@post) or post_url(@post)<\/strong>, you can use <strong>article_path(@post) or article_url(@post)<\/strong>.<\/p>\n<p>Here&#8217;s how you can use these named route helpers in your controllers, views, or other parts of your Rails application:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\n# In a controller\r\nredirect_to article_path(@post)\r\n\r\n# In a view\r\n<%= link_to 'View Post', article_path(@post) %>\r\n<\/pre>\n<p>This can be used when we want to change the name of our resources in future without having to update all the places where you reference them in your application. It provides an abstraction layer to our routes.<\/p>\n<p>Keep in mind that the :as option is optional, and if you don&#8217;t specify it, Rails will automatically generate named route helpers based on the resource name. Using :as is beneficial when you want more control over the names of your route helpers.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/03\/Screenshot-from-2023-11-21-10-37-32-1024x507.png\" alt=\"With :as option for posts\" width=\"1024\" height=\"507\" class=\"alignnone size-large wp-image-9725\" srcset=\"https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/03\/Screenshot-from-2023-11-21-10-37-32-1024x507.png 1024w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/03\/Screenshot-from-2023-11-21-10-37-32-300x149.png 300w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/03\/Screenshot-from-2023-11-21-10-37-32-768x380.png 768w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/03\/Screenshot-from-2023-11-21-10-37-32.png 1284w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/03\/Screenshot-from-2023-11-21-10-37-59-1024x506.png\" alt=\"Without :as option for posts\" width=\"1024\" height=\"506\" class=\"alignnone size-large wp-image-9726\" srcset=\"https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/03\/Screenshot-from-2023-11-21-10-37-59-1024x506.png 1024w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/03\/Screenshot-from-2023-11-21-10-37-59-300x148.png 300w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/03\/Screenshot-from-2023-11-21-10-37-59-768x379.png 768w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/03\/Screenshot-from-2023-11-21-10-37-59.png 1276w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Another example with match in routes.rb file<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nmatch '\/search' => posts#search', as: 'post-search', via: %i[get post] # PostsController#search\r\n<\/pre>\n<p>Above will create post_search_path and post_search_url helpers for \u201c\/search\u201d routes with get and post requests.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>:as option is used to create named routes in rails. It allows you to specify a name for a route.These helpers make it easier to generate URLs and paths within your application, especially when dealing with resourceful routes. Example:- # config\/routes.rb Rails.application.routes.draw do resources :posts, as: &#8216;articles&#8217; # &#8216;articles&#8217; is the custom name for the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9724,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-9723","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9723"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9723"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9723\/revisions"}],"predecessor-version":[{"id":9727,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9723\/revisions\/9727"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9724"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}