Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Long answer</strong></p> <p>Rails generates a number of handy named routes for you when you add routes to your routes.rb file. Usually when in doubt for routes I take a look at my rake routes task which shows you a list of all available routes. Try running <code>rake routes &gt; routes.txt</code> and open up the corresponding routes.txt file. </p> <p>The resulting file will list out a series of requests for you, in your case you should see something similar to this for your microposts controller:</p> <blockquote> <pre><code> POST /microposts(.:format) microposts#create micropost GET /microposts/:id(.:format) microposts#show DELETE /microposts/:id(.:format) microposts#destroy </code></pre> </blockquote> <p>Rake routes produces the following information for each of your routes (where applicable):</p> <ol> <li>The route name (if any)</li> <li>The HTTP verb used (if the route doesn’t respond to all verbs)</li> <li>The URL pattern to match</li> <li>The routing parameters for the route</li> </ol> <p>With that information in mind be can simply look at the urls provided in the routes.txt file for the url we're trying to get to (/microposts/25). You'll see that the listed <code>/microposts/:id(.:format)</code> url pattern handles that perfectly. Lo and behold it also maps to the microposts#show action that you want so now to get the named route just look at the first column to appear and you'll see the "microposts" keyword. Simply add _path` to this and you'll have your named route usable in views to generate link urls. Since this particular route requires an id parameter (as detailed in the url pattern) you have to pass the named route helper and id argument as well. </p> <p>Also in your routes.rb file when you add <code>resources :something</code> it generates routes for each of the default seven RESTful routes (new, create, edit, update, delete, index, show). In your case you're explicitly telling rails to generate default routes for the actions create, destroy and show so you can erase the line at the bottom <code> match "/microposts/:id" =&gt; "microposts#show"</code> because that's already being handled. </p> <hr> <p><strong>Short answer</strong></p> <p>Change this:</p> <pre><code>&lt;%= link_to feed_item.title, @micropost %&gt; </code></pre> <p>To this:</p> <pre><code>&lt;%= link_to feed_item.title, micropost_path(feed_item) %&gt; </code></pre> <p>See <a href="http://guides.rubyonrails.org/routing.html" rel="nofollow">Ruby on Rails Guides: Rails routing from the Outside In</a> for all you need to know about routes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload