Search code examples
ruby-on-railsroutesruby-on-rails-5custom-routes

Define custome routes in ruby on rails


I wants to process multiple url segments with single controller and function here is example:

1) https://www.somedomain.com/abc and

2) https://www.somedomain.com/xyz/abc

how can i define the routes in one line, so both request can be performed with single function

like here i need to define two routes get "/:static_page", to: "SomeController#action_name"

get "/:static_page/:second_option", to: "SomeController#action_name"

But how can i define in one line? or can manage this approach any other way.


Solution

  • Its called a catch-all and is a superb way to shoot yourself in the foot.

    match '*path', to: 'some#action_name'
    

    You have to pay really close attention to ordering (declare it last) as it will catch any other routes that match the pattern - in this case everything.

    Its also a huge source of bugs as Rails now will take any request thrown at it and pass it to your controller instead of raising a routing error. Like for example those pesky bots that are trolling your site for WordPress vulnerabilites.

    If you really need some additional non-restful routes just declare them normally instead of funneling everything into what is going to become a monstrosity of a controller.