Nested routes with hash_routes

58 views
Skip to first unread message

Shreko

unread,
Jul 1, 2021, 11:47:39 AMJul 1
to Roda
I'm using hash_routes and trying to make routes in form:
cats/:cat_id/products
cats/:cat_id/products/new
cats/:cat_id/products/:product_id
... and so on.

In cats hash_routes I have
```
class App
  hash_routes.on 'cats' do |r|

    r.on Integer do |id|
     
      r.is do
       
        r.get do
          puts "show view"
         
        end

      end

      r.on 'products' do
        puts "Entering Products Here"
        r.hash_routes('products')
      end

    end

  end
end  
```

Then in  products hash_routes I have
```
Class App
  hash_routes.on 'products' do |r|
   
    puts "Entering hash route products"
    r.is do

      r.get do
        p "show these products"
       
      end
    end
  end
end
```
I get  404 when I try route cats/1/products

Thanks

Jeremy Evans

unread,
Jul 1, 2021, 12:23:33 PMJul 1
to ruby...@googlegroups.com
This issue is because:

  r.hash_routes('products')

Delegates to hash routes in the "products" namespace, while:

  hash_routes.on 'products' do |r|

Defines a hash route named products in the default namespace.

You should switch to something like the following, having a namespace for cat routes, and dispatching to the products route inside the cat namespace:

class App < Roda
  plugin :hash_routes

  route do |r|
    r.hash_routes
  end


  hash_routes.on 'cats' do |r|
    r.on Integer do |id|
      @cat_id = id

      r.is do
        r.get do
          puts "show view"
          "cat #{@cat_id}\n"
        end
      end

      r.hash_routes(:cat)
    end
  end

  hash_routes(:cat).on('products') do |r|

    puts "Entering hash route products"
    r.is do
      r.get do
        p "show these products"
        "cat #{@cat_id} products\n"
      end
    end
  end
end

Thanks,
Jeremy

Shreko

unread,
Jul 1, 2021, 1:52:46 PMJul 1
to Roda
It works now. Thank you very much Jeremy for all your help all these years. I love your work and use sequel and roda in all my projects, but I still find roda routing hard to understand, even with your excellent documentation. Yesterday I was reading the documentation and at one point in frustration, I just thought, just give me back sinatra/padrino routing, I don't care about performance of the routing tree. But knowing your work, you made this for a reason so I will stick to it. All these DSLs and metaprogramming used makes it harder to understand. I don't know if anything can be done to improve, maybe you can show us how to debug these r. and like objects and find out which object responds to what on a basic level for someone who doesn't really understand much of metaprogramming.

Thank you very much.

Jeremy Evans

unread,
Jul 1, 2021, 3:46:36 PMJul 1
to ruby...@googlegroups.com
On Thu, Jul 1, 2021 at 10:52 AM Shreko <shre...@gmail.com> wrote:
It works now. Thank you very much Jeremy for all your help all these years. I love your work and use sequel and roda in all my projects, but I still find roda routing hard to understand, even with your excellent documentation. Yesterday I was reading the documentation and at one point in frustration, I just thought, just give me back sinatra/padrino routing, I don't care about performance of the routing tree. But knowing your work, you made this for a reason so I will stick to it. All these DSLs and metaprogramming used makes it harder to understand. I don't know if anything can be done to improve, maybe you can show us how to debug these r. and like objects and find out which object responds to what on a basic level for someone who doesn't really understand much of metaprogramming.

If you don't need the performance or maintainability advantages of the routing tree design, try using the class_level_routing plugin.  It offers a more Sinatra-like interface, where routes are defined at class level.  Like Sinatra, it uses a linear lookup for routes, but if you don't have a large number of routes and routing performance isn't critical, it should be fine.  You can even mix the routing tree methods inside these class-level methods.

In terms of the objects used, the r. methods are documented at http://roda.jeremyevans.net/rdoc/classes/Roda/RodaPlugins/Base/RequestMethods.html.  The methods directly callable inside the route block are documented at http://roda.jeremyevans.net/rdoc/classes/Roda/RodaPlugins/Base/InstanceMethods.html.  Each plugin that adds methods to these classes will also have documentation for the methods (in addition to documentation for the plugin itself).  For example, with the assets plugin (http://roda.jeremyevans.net/rdoc/classes/Roda/RodaPlugins/Assets.html), you can look under "Classes and Modules" to find the method documentation.

I'm also not sure if something can be done to improve the documentation, but I'm open to suggestions.

Thanks,
Jeremy

Billy.Zheng

unread,
Jul 20, 2021, 12:43:15 PMJul 20
to ruby...@googlegroups.com, Jeremy Evans
Maybe a can try give some suggestions.

> I don't know if anything can be done to improve, maybe you can show us how to debug these
> r. and like objects and find out which object responds to what on a basic
> level for someone who doesn't really understand much of metaprogramming.

I think you have to do this youself, there is a gem name `looksee` can help on this a lot i think.

> I'm also not sure if something can be done to improve the documentation,
> but I'm open to suggestions.

I consider `programming roda` is a nice book for beginning roda user, sure it can answer question like this.
For a more deeper reading, i thought comment in plugin is very well too.

--
Billy
Reply all
Reply to author
Forward
0 new messages