Skip to content

url helper for redirects and links #6

@ericgj

Description

@ericgj

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions