Roda ending slash behavior when visit a url.

22 views
Skip to first unread message

Billy Zheng

unread,
Jun 10, 2021, 2:51:48 PM6/10/21
to Roda

Hi, i don't know if this is a issue, so asking here.

  class App < Roda
    route do |r|
      r.on "posts" do
        post_list = {
          "1" => "Post[1]",
          "2" => "Post[2]",
          "3" => "Post[3]"
        }

        r.is "" do
          post_list.values.join(' | ') # when visit with GET /posts/, it get here.
        end

        r.is :id do |id|
          post_list[id]
        end

        "hello" # but when visit with  GET /posts (no ending slash), it get here.
      end
    end
  end

i am new to roda, but, i think this is a strange behavior, is this behavior is intended? and why?

thank you.


Jeremy Evans

unread,
Jun 14, 2021, 11:57:33 AM6/14/21
to ruby...@googlegroups.com
This is expected behavior.  "GET /posts" and "GET /posts/" are two completely different requests, and Roda treats them differently. If you would like them treated the same, the slash_path_empty plugin can help with that. However, you should use `r.is` instead of `r.is ""` in your example in that case (`r.is ""` requires that there be an additional segment).  The slash_path_empty plugin will make Roda consider a remaining path of "/" the same as an empty path.

Thanks,
Jeremy
Reply all
Reply to author
Forward
0 new messages