dennismonsewicz
dennismonsewicz

Reputation: 25542

Rails Routing with Optional Params on Root URL

I am working on an app that allows for a user to go to the root url like so: http://localhost:3000/param_here

My Routes are setup like the following:

match '/:test' => redirect { |params| "/?id=#{params[:test]}" }

The problem with this is that my other routes are being picked up and being redirected as well.

Full Route.rb

Myapp::Application.routes.draw do
  match '/:test' => redirect { |params| "/?id=#{params[:test]}" }
  match '/aboutus', to: 'pages#aboutus', as: "about_us"

  # Set the Home Page to a static page  
  root :to => 'pages#home'
end

So if a user goes to /aboutus, the redirect picks it up and assumes it should be set as the id. I still would like to use regular routes with also achieving this redirect

Upvotes: 0

Views: 1286

Answers (2)

sjain
sjain

Reputation: 23344

match '/:test' => redirect { |params| "/?id=#{params[:test]}" }, :defaults => { :id => '#{params[:test]}' }

Upvotes: 1

ply
ply

Reputation: 1141

Have you tried moving the /aboutus route rule above the

  match '/:test' => redirect { |params| "/?id=#{params[:test]}" }

rule in your routes file?

Routes flow from the top down so if you move your /about us route above the route with the optional params it should dispatch your request to the first matching route, in this case the /aboutus route

Upvotes: 2

Related Questions