{"id":2633,"date":"2022-08-30T15:26:29","date_gmt":"2022-08-30T15:26:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/where-is-this-posts_path-variable-defined-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:29","modified_gmt":"2022-08-30T15:26:29","slug":"where-is-this-posts_path-variable-defined-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/where-is-this-posts_path-variable-defined-collection-of-common-programming-errors\/","title":{"rendered":"Where is this &ldquo;posts_path&rdquo; variable defined?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m following this tutorial (seems good) for Rails. After I run<\/p>\n<pre><code>ruby script\/generate scaffold Post\n<\/code><\/pre>\n<p>then this link works in one of the erb files:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p><strong>WHY?<\/strong> I&#8217;ve looked for &#8220;posts_path&#8221; in the whole app and it&#8217;s nowhere to be found. On the other hand, this<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>does <strong>not<\/strong> work, and it&#8217;s also a Controller.<\/p>\n<p>Where is the <code>posts_path<\/code> defined?<\/p>\n<ol>\n<li>\n<p><code>posts_path<\/code> is a named route you get for free from the route that was added by <code>script\/generate scaffold<\/code>. See <code>routes.rb<\/code> you should see something like this:<\/p>\n<pre><code>map.resources :posts\n<\/code><\/pre>\n<p>See the API docs for information on what other named routes you get for free.<\/p>\n<p>Also you can run <code>rake routes<\/code> and see what all your <code>routes.rb<\/code> is giving you.<\/p>\n<p>If you want a home_path named route add a line like this to your <code>routes.rb<\/code>:<\/p>\n<pre><code>map.home '\/home', :controller =&gt; \"home\", :action =&gt; \"index\"\n<\/code><\/pre>\n<\/li>\n<li>\n<p>I believe that &#8220;posts_path&#8221; is created dynamically by Rails at runtime. Look at your routes.rb file &#8211; Home is probably not defined the same way as Posts. It has nothing to do with you controllers, it&#8217;s dependent on the route definition.<\/p>\n<\/li>\n<li>\n<p><code>map.root :controller =&gt; \"home\"<\/code> would be a shorter way of writing the path to your home directory. This will use \/ has the home, and not \/home. If you still want to use \/home (and home_path), <code>map.home 'home', :controller =&gt; \"home\"<\/code> will do the same thing.<\/p>\n<p>There&#8217;s a great guide written by Mike Gunderloy about everything there is to know about routing.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-04 10:08:08. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m following this tutorial (seems good) for Rails. After I run ruby script\/generate scaffold Post then this link works in one of the erb files: WHY? I&#8217;ve looked for &#8220;posts_path&#8221; in the whole app and it&#8217;s nowhere to be found. On the other hand, this does not work, and it&#8217;s also a Controller. Where is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2633","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2633"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2633\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}