Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The router cannot tell the difference between a :language and a :tag.</p> <p>Just because your routes say "language" and "tag" when you are constructing your code in the view.. remember that in the html this has been translated into just plain ole URLs eg /spanish or /rails</p> <p>the route then has to be figured out from this URL.</p> <p>Now as I said, the router can't tell that a particular word <em>is</em> a language or a tag... and the plain-ole-URL doesn't have the word "tag" or "language" in it anymore... so your two routes here:</p> <pre><code>match '/:language', :to =&gt; 'posts#search_result' match '/:tag', :to =&gt; 'posts#search_result' </code></pre> <p>are both the <em>same</em> kind of URL</p> <p>Just a single token after the slash. Here are some examples that will match that route:</p> <pre><code>/greek /spanish /rails /urdu /whatever </code></pre> <p>They will <em>all</em> match the first route that matches on "a single token after a slash"... which means your router will match all of them to the "language" route and will never ever match the "/:tag" route, because it's already matched on the route above.</p> <p>he he: it's all greek to the router ;)</p> <p><strong>Edit:</strong></p> <blockquote> <p>Hi, this is helping me a lot to understand how routing works.. but still i can't see it clear. I understand what you said, and so basically i understand i should do something like match '/tags/:tag to at least only route to posts#search_result the URLS starting by /tag .. what would be a solution??</p> </blockquote> <p>yes, "/tags/:tag" would be clear and unambiguous, but if you want it to truly flexible in tag vs language you would be better served by the simple:</p> <pre><code>match '/posts/search', :to =&gt; 'posts#search_result' </code></pre> <p>which can use any of your link_to examples above to generate eg:</p> <pre><code>/posts/search?tag=rails /posts/search?language=spanish /posts/search?language=spanish&amp;tag=rails </code></pre> <p>It's also far more clear what is being passed and <em>why</em>.</p> <p>The description of the third URL is "I'm searching for a set of posts which have language = spanish and tag = rails"</p> <p>Your URL should reflect the <em>resource</em> (which in this case is a set of posts) everything else is better done as query params.</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