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.
Related
So, I have a named route:
match 'ip/get/:ip' => 'ip_addresses#show', :via => :get
As you can see, I'd like the ip (after 'get') to be dynamic, but I keep getting a routing error when I try it out. Here are my routes:
root / ip_addresses#index
ip_add POST /ip/add(.:format) ip_addresses#create
GET /ip/add(.:format) ip_addresses#new
ip_all GET /ip/all(.:format) ip_addresses#index
GET /ip/get/:ip(.:format) ip_addresses#show
DELETE /ip/all(.:format) ip_addresses#destroy
And here's my show action:
def show
IpAddress.find(params[:id])
end
EDIT: Routing error:
ActionController::RoutingError (No route matches [GET] "/ip/get/1.2.3.4"):
I've read the Rails Routing from the Outside In Guide (http://guides.rubyonrails.org/routing.html) but naturally I may be overlooking something. Any help is appreciated. Thanks!
The answer to your question lays in article you gave.
Take a look at section:
By default dynamic segments don’t accept dots – this is because the
dot is used as a separator for formatted routes. If you need to use a
dot within a dynamic segment add a constraint which overrides this –
for example :id => /[^/]+/ allows anything except a slash.
Look at the example there:
match ':controller(/:action(/:id))', :controller => /admin\/[^\/]+/
So in your example I believe it would be:
match 'ip/get/:ip' => 'ip_addresses#show', :id => /[^/]+/ , :via => :get
And also change params[:id] to params[:ip]
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.
I looked on the web for a while but I can't get this to work. Our application has to work with urls like ourapp.com/meandyou, where the common element is the "and" in the parameter.
I saw that it's possible to constrain urls parameters using regex, so I added the rule to routes.rb, but without success. If I try to match the same expression using the terminal, it works. Here's the complete route file:
Railroot::Application.routes.draw do
resources :couples
get "home/index"
root :to => 'home#index'
match ':url' => 'couples#show_url', :url => /and/
end
I read that Rails nests the expression within a bigger one when matching the route, so maybe I'm doing something slightly wrong even for such a simple expression.
I'm running on Ubuntu 10.04, Ruby 1.9.3, Rails 3.2.3, Passenger 3.0.13, Nginx 1.2.1.
Thanks in advance for your help!
This should be your starting point:
Railroot::Application.routes.draw do
root :to => 'home#index'
resources :couples
match ':url' => 'couples#show_url', :constraints => { :url => /and/ }
end
This may be your answer, from the rails routing docs:
:constraints takes regular expressions with the restriction that regexp anchors can’t be used. [...]
However, note that you don’t need to use anchors because all routes are anchored at the start.
So I think what you are actually matching against is not /and/ but /^and/, which would explain why it's not working.
Try being more explicit, like this:
match ':url' => 'couples#show_url', :url => /.*and/
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
I'm trying to match a URL something like
http://example.com/this.is.my.full.hostname/something/else
apparently when I pass the param in the routes.rb file it doesn't recognize this parameter
my code says the following
match '/:computer_hostname/somethingelse' => 'test#info'
any ideas what's the right way to achieve the URL I wanted above ? Or is it even possible ? I know period is allowed character in URL but does it allow more than one ?
I think the constraints method/option will help you out. Try something like the following:
match ':hostname/something/else' => 'test#info',
:constraints => {:hostname => /[A-Za-z0-9\._\-]+/}
If you're doing multiple matches all with the same :hostname segment, then you can wrap them in a constraints method call:
constraints(:hostname => /[A-Za-z0-9\._\-]+/) do
match ':hostname/something/else' => 'test#info'
match ':hostname/foo/bar' => 'test#foo'
end