Following the example on https://github.com/soveran/cuba#usage and changing it a little:
Cuba.define do
on get do
on 'home' do
res.write "Aloha!"
end
end
on 'api' do
on get do
on "home", params("a"), param("b") do |a,b|
res.write "Hello World!"
end
end
end
end
Test:
scope do
test "Homepage" do
get "/api/home?a=00&b=11"
assert_equal "Hello World!", last_response.body
end
end
But I get an Assertion Failed:
AppTest.rb:15:in `block (2 levels) in <main>': "Hello world!" != "" (Cutest::AssertionFailed)
It is like that the url which I use in the test is not right. How should I change it?
Update:
I noticed that if I change the definition then it works correctly:
Cuba.define do
on 'api' do
on get do
on "home", params("a"), param("b") do |a,b|
res.write "Hello World!"
end
end
end
on get do
on 'home' do
res.write "Aloha!"
end
end
end
I would suggest you structure your routes in a different way to keep things clean and maintainable.
For instance, you could do:
Also, remember that when you use the
parammatcher you're saying that the param must be there for the route to match. Sometimes this is what you want. However, if the query parameter is optional, you would access it throughreq: