codereading / cuba

Rum based microframework for web development.
http://cuba.is
MIT License
6 stars 2 forks source link

url helper for redirects and links #6

Open ericgj opened 12 years ago

ericgj commented 12 years ago

Maybe you can convince me I don't need such things in Cuba, but with Sinatra I use the url() AKA to() helper method quite a bit. Especially when I am mounting apps with Rack::URLMap. It lets me change the 'mount-point' without breaking the links and redirects spread all over my app. It's the equivalent of Rails' url_for without the overhead of controller classes.

I don't see how you could have such a method in Cuba very easily, given there isn't this concept of fixed top-level mount-points and the SCRIPT_NAME is adjusting all the time. I suppose in your top-level app, you could have some settings like

Cuba.settings[:mounts] = {
  admin: "admin",
  blog: "blog",
  api: "api",
  app: ""
}

And then some helpers that uses these settings like

res.redirect to_mount(:blog)               # res.redirect '/blog'
res.redirect to_mount(:app, "dashboard")  # res.redirect '/dashboard'

on mount(:admin) { }        #  on 'admin'

So then when you decide you have to re-organize or rename the mount-points, you only have to change the strings in one place.

But is there another simple alternative to using full path strings in links and redirects?

@codereading/readers