Rails 3 Routing based on regex - ruby-on-rails

I want to redirect all routes that start with the string xyz to some other path.
match /\/xyz\/(.)*/ => redirect{ "whateverurl" }
The match method doesn't seem to work when given a regex, I have googled alot seems like there are options to do with regex but they are for params for example
match 'photos/:id' => 'photos#show', :constraints => { :id => /[A-Z]\d{5}/ }
How can I achieve it ?

How about:
match '/xyz/*foo' => redirect('url')
It's not a regexp, it's called route globbing. More about it here.

Related

Ruby on Rails - Regular expression for error routes

How do I define a routes match any things exclude string ( like 'websocket' )?
Thanks!
Based on the comments, it sounds like you want to match /websocket to a specific action and everything else to an 404 error page.
Utilizing the fact that routes are matched in the order they are defined in routes.rb, this is a good approach to do it:
match '/websocket' => 'controller#action'
match '/:slug' => "errors#show", :code => 404, :via => [:get]
When a request /string comes, the routing subsystem will first try to match it to the first line, and if string is equal to websocket then the match is successful and no more routes will be matched.
If string is not websocket on the other hand, then it will match the second line.

rails sunspot gem friendly SEO url's?

How does one get SEO friendly url's in sunspot?,
The method of search form is GET as suggested from the rails docs, but now i have a very long query string that looks terrible, is it possible to have it something like
/search/param1/bla/param2/bla
instead of the long
?search&param1=somevalue&param2=someval2
You could modify your search route to include the params. Something like this within your routes.rb:
match '/search/:param1/:param2' => 'search_controller#search_action', :as => :search_with_params, :via => :get
Then a user would visit:
/search/value1/value2
Which gives you params[:param1] and params[:param2] to access the values from the url.
If those 2 params are not required for all searches I believe you would also need a route just for the search action:
match '/search' => 'search_controller#search_aciton', :as => :search, :via => :get

How to match hash (deep nested) params in Rails3 to make a pretty URL?

If I have this route (in routes.rb):
match 'posts', :to => 'posts#index'
It will show and match the following routes:
# Case 1: non nested hash params
posts_path(:search => 'the', :category => 'old-school')
#=> "/posts?search=the&category=old-school"
# Case 2: nested hash params
posts_path(:filter => {:search => 'the', :category => 'old-school'})
#=> "/posts?filter[search]=the&filter[category]=old-school"
If I want to make the category param part of the main URL, I could do this for the Case 1.
match 'posts(/:category)', :to => 'posts#index'
that will show and match the following routes:
# Case 1: non nested hash params
posts_path(:search => 'the', :category => 'old-school')
#=> "/posts/old-school?search=the"
But how could I do the same if the param is nested (Case 2)?
I would expect the next route definition:
match 'posts(/:filter[category])', :to => 'posts#index'
to work this way:
# Case 2: nested hash params
posts_path(:filter => {:search => 'the', :category => 'old-school'})
#=> "/posts/old-school?filter[search]=the"
But it does not work.
I found this same question in two places with no righ answer:
how-to-specify-nested-parameters-in-the-routes
how-to-accept-hash-parameters-in-routes
The Rails Guides don't specify anthing about this.
Should I assume that this can not be done in rails? really?
you could just make two different routes instead
match 'posts', :to => 'posts#index'
match 'posts/:category', :to => 'posts#index'
The next route will not work as you intended it.
match 'posts(:filter[category])', :to => 'posts#index'
The :filter is just a place holder for either the first argument thats passed into the url helper or the value for the key :filter in a has that is passed in. Any expressions in the route string will not be evaluated.
I guess the answer to your question is that you cannot do this in rails. I would suggest to you though that you do this in another way. It is very helpful in rails to follow the convention and make things easier on yourself.
Looks like you are doing three things here. The base post routes
match 'posts', :to => 'posts#index'
A route that has the category nested in it. Most likely to give the user a better url
match 'posts/:category', :to => 'posts#index'
And a search url which can be the same as the first, or to make your action cleaner, a different one
match 'posts/search', :to => 'posts#search'
There is really no reason I can think of to complicate the routes in the way your are suggesting. A search query url doesn't look nice anyways so why bother handling two urls for searches. Just one will do.
You should definitely take a look at running
rake routes
as this will tell you exactly what you have defined in your routes file. You can also set up routing tests to ensure your custom routes are performing correctly.
Your example does not work (as you indicated)
# Case 2: nested hash params
posts_path(:filter => {:search => 'the', :category => 'old-school'})
#=> "/posts/old-school?filter[search]=the"
But what you should be looking for is this
posts_path(:filter => {:search => 'the', :category => 'old-school'})
#=> "/posts?filter[search]=the&filter[category]=old-school"
This is ok to do it this way.
If you want to keep posts/:category just use this for navigation only, not for search.
Hope that helps

Rails routes match starting with a numeric character

I have a an url like "http://domain.com/1and2" that I wanted to set up in config/routes.rb like this:
match "1and2" => "frontpage#oneandtwo"
(with controllers and views in place).
Running 'rake routes' outputs 'Invalid route name: '1and2''. This error is apparently triggered when you start a match with a numeric character.
Is there a workaround, or am I doing it wrong?
match '/:id' => "frontpage#oneandtwo", :constraints => {:id => /1and2/}
The root of the problem is that methods in Ruby cannot start with a number. Since Rails routing will generate an accessor method for each route, you'll get an error.
You can pass by the issue by naming your route differently with the :as parameter.
I had an issue where I wanted to redirect from a URI /2012 -- which resulted in an error. I corrected it by adding :as => current_year to the routing:
match "/#{Time.now.year}" => redirect("..."), :as => :current_year
Further information:
https://github.com/rails/rails/issues/3224

rails routes problem

here are my routes.i want to prevent duplicate url
match ':id' => 'people#show'
match 'people/:id' => redirect("%{id}")
How about?
match '(people)/:id' => 'people#show'
Bound Parameters

Resources