Support for URLs like / like: product in Ruby on Rails?

I am trying to use routes.rb to create a URL / similar product: (where the product is dynamic) for my site. The problem is that route.rb easily supports URLs such as /: product-like, but it doesn’t support the first because it requires: the preceding product separator ('/' is the separator, and '-' is not The list of delimiters is in ActionController :: Routing :: SEPARATORS.

I cannot add '-' as a separator, because: the product may also contain a hyphen. What is the best way to support such a URL?

One of the methods that I have successfully tried is not to use route.rb and put the parsing logic in the controller itself, but this is not the cleanest way.

+3
source share
4 answers

In fact, you can add -as a separator and then use routing.

map.similar_product '/similar-to-*product', :controller => 'products', :action => 'similar'

then in ProductsController # similar

@product = Product.find_by_slug params[:product].join('-')

Although refactoring seems more enjoyable, since with this approach you need to specially process all the slugs that may contain hyphens.

+1
source

I would reorganize your urls so that they are just "/ product similar"

+2
source

. . README.

With a routing filter, you can have a url /similar-to-:product, pre-process it before /similar-to/:productit receives routing recognition. You will also want to post-process the created paths from /similar-to/:productto /similat-to-:product.

+1
source

I'm a little confused, but how could you add “to-” as a separator?

0
source

Source: https://habr.com/ru/post/1697378/


All Articles