Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Base.html#method-i-match" rel="nofollow"><code>match</code> method</a> should recognize the <code>:id</code>:</p> <blockquote> <p>Any options not seen here are passed on as params with the url.</p> </blockquote> <p>I don't know why <code>:id</code> and <code>:page_id</code> are being treated differently. You could use <a href="http://guides.rubyonrails.org/routing.html#defining-defaults" rel="nofollow"><code>:defaults</code></a> to set default values in <code>params</code> though:</p> <blockquote> <p>You can also define other defaults in a route by supplying a hash for the <code>:defaults</code> option. This even applies to parameters that you do not specify as dynamic segments.</p> </blockquote> <p>So try this:</p> <pre><code>Page.all.each do |page| page.url = "/" + page.self_and_ancestors.map { |p| p.slug }.join("/") page.save match page.url =&gt; "pages#show", :defaults =&gt; { :id =&gt; page.id } end </code></pre> <p>Note that that will update each <code>page</code> every time your <code>routes.rb</code> is loaded and that might not be what you're looking for. You might want to put the URL setting elsewhere and just have this:</p> <pre><code>Page.all.each do |page| match page.url =&gt; 'pages#show', :defaults =&gt; { :id =&gt; page.id } end </code></pre> <p>in your <code>routes.rb</code>.</p> <p>The usual approach for this would be to have one route:</p> <pre><code>get '/:slug' =&gt; 'pages#show', :constraints =&gt; { :slug =&gt; %r([\p{Alnum}_.-/]+) } </code></pre> <p>And then use the <code>:slug</code> instead of the <code>:id</code> to find the page:</p> <pre><code>def show @page = Page.find_by_url(params[:slug]) #... end </code></pre> <p>Then you don't have to worry about dynamic routes at all, you just need to make sure the <code>url</code>s are right and that they're indexed. The precise form of the <code>:constraints</code> that you use would depend on what your <code>url</code> values look like. You'll also want to put this route at the bottom of your <code>routes.rb</code>.</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