Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting regex for path parts #16

Open
paulcsmith opened this issue Apr 5, 2020 · 2 comments
Open

Allow setting regex for path parts #16

paulcsmith opened this issue Apr 5, 2020 · 2 comments

Comments

@paulcsmith
Copy link
Member

No description provided.

@cristian-lsdb
Copy link

This would be especially useful when a wildcard route unintentionally matches a static asset, leading to a 404 error.

For example:

class Products::Show < BrowserAction
  get "/:brand/:ref" do
    product = ProductQuery.new.brand(brand).ref(ref).first
    html ShowPage, product: product
  end
end

In this case, the route get "/:brand/:ref" matches /css/app.css, but no Product with the brand "css" and ref "app.css" exists, resulting in an Avram::RecordNotFoundError.

In Rails, this problem can be avoided by adding constraints on routes:

get "/:brand/:ref", to: "products#show", constraints: { brand: /^(?!css).*$/ }

In this discussion @jwoertink suggest the possibility to use annotations.

@[Lucky::RouteField(ignore: /^(?!css).*$/)]
get "/:brand/:ref" do
  html ShowPage
end

@paulcsmith
Copy link
Member Author

I like that. I also wonder if we could use a regex directly in the path. But I think either approach would be nice to have!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants