Route match to dynamic actions in rails - ruby-on-rails

Can I write one row route for below two rows?
match "/article/:id/" => "articles#redirect"
match "/articles/:id/" => "articles#redirect"
I tried something like
match "/:article_redirect/:id/" => "articles#redirect", :constraints => {:article_redirect => /[article|articles]/}
But it didn't work.

I think your regexp id not good: try
match "/:article_redirect/:id/" => "articles#redirect", :constraints => {:article_redirect => /(article|articles)/}
that is (article|articles) instead of [article|articles]

Related

Routing with regex condition in Rails 2

In my routes.rb file, I've defined these routes:
map.with_options(:controller => "review") do |review|
review.review_index "/review/:page", :action => "index", :defaults => {:page => nil}, :requirements => {:page => /[0-9]./}
review.review_provider "/review/:category_name/:page", :action => "provider", :defaults => {:page => nil}
end
However, it only match with the second routes.
For example,
/review/1
must match with first rule but in fact it is matched with the second rule.
How can I config it so that:
/review/1 will match with the first rule
/review/a_category/1 will match with the second rule
Your regular expression in your first route is bad. Period matches any single character.
/[0-9]/ means "any number, followed by any other single character".
So, that would match /review/1a, /review/70, /review/7?, etc,
If you want to match one or more digits, change your regular expression to: /[0-9]+/

Error using wildcards and redirect on routes in Rails

match "/myroute*" => redirect("http://google.com"), :as => :myroute
The line above in routes.rb is causing the following error
/Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/racc/parser.rb:349:in `on_error': (Racc::ParseError)
parse error on value ")" (RPAREN)
from /Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/racc/parser.rb:99:in `_racc_do_parse_c'
from /Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/racc/parser.rb:99:in `do_parse'
Looks like it is because I'm adding a wildcard (*). Any idea how to solve this?
Wildcard components need to have a "label" as well, e.g.
match "/myroute*something" => redirect("http://google.com"), :as => :myroute
will match /myrouteblah and /myroute/hello/world where params[:something] is blah and /hello/world respectively.
EDIT: Check out http://guides.rubyonrails.org/v3.2/routing.html#route-globbing if you haven't already.
Try this:
match ':redirect' => redirect("http://google.com"), :as => :myroute , :constraints => { :redirect => /myroute.?/i }

Rails 3 Routing based on regex

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.

routing to page name

is there a way i could route - http://localhost:3000/pages/1 to http://localhost:3000/home and all the other pages in my site i.e. - http://localhost:3000/pages/4 to http://localhost:3000/contact-us automatically?
I can do this the other way around using -
match "/home" => 'pages#show', :id => 1
match "/cars-for-sale" => 'pages#show', :id => 1
match "/contact-us" => 'pages#show', :id => 4
but need to do this in revers and automatically if possible.
Perhaps what you really need is a redirect:
match "/pages/1", :to => redirect("/home")
match "/pages/:id", :to => redirect("/contact-us")
Note, that the order is significant - "Rails routes are matched in the order they are specified" (see http://edgeguides.rubyonrails.org/routing.html)

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