map.resource, parameter restrictions - ruby-on-rails

I've a controller :platform here.
I'm trying to do something like:
/:platform_name/ to redirect to its show, with the parameter. Here is what I've got:
map.resource :platform,
:as => ':platform_name',
:platform_name => /pc|ps2|ps3|wii|ds|psp|xbox-360/
It's working fine. I've other neasted resources to it, and all them are accessing. But.
The problem is, I've only those platform names, but when it doesnt fine another route, it aways fall on this.
if I try /whatever/, it will look for the platform_name => whatever.
I was expecting it to fall into the map.connect ':controller/:action/:id' rule.
When I did :platform_name => /pc|ps2|ps3|wii|ds|psp|xbox-360/, wasnt expected that this rule only apply when the regular expression is fit?
how could i restrict this?

Try changing:
/pc|ps2|ps3|wii|ds|psp|xbox-360/
to
/^(pc|ps2|ps3|wii|ds|psp|xbox-360)$/

Maybe you could try
:requirements => { :platform_name => /pc|ps2|ps3|wii|ds|psp|xbox-360/ }
or
:conditions => { ... }
ActionController::Resources#resources

Related

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 routes constraints based on model

Using Rails 3.1. I have the following in my route:
In my model Shop, I have a column called shop_type that has either boutique or saloon. Instead of having the url:
http://localhost/spots/1
I would like to separate it by the shop_type to:
http://localhost/boutique/1
http://localhost/saloon/2
So I added the following to my route:
resources :boutique, :controller => 'shops', :constraints => { :shop_type => 'boutique' }
resources :saloon, :controller => 'shops', :constraints => { :shop_type => 'saloon' }
The problem with this is I can access record ID 1 with shop_type = boutique with either of the URL. Ideally, it should return error when a user tries to access
http://localhost/saloon/1
But the above URL just works fine, which is not what I want.
Also, is there anyway to redirect all shops/1 to the new URL which is by shop_type?
Many thanks.
If you want to do this, then your application is probably telling you that it really wants two separate classes, Boutique and Saloon. Can you do that? If not, why not?
Maybe its better to tell Rails directo allow urls as:
get "/:shop_type/:id", :to => 'shop_controller#show'
And in controller check if the record exist, and return :status => 404 if not:
#shop = Shop.where(:id => params[:id], :shop_type => params[:shop_type]).first
render :status => 404 and return if #shop.nil?
Note that route provided is too greedy and put it after all other routes so it will not 'eat' other request.

Handle rails route with GPS parameter

I'd like to create a route in my rails app to handle a gps-coordinate parameter. The intention is to find restaurants near the given position.
This is were I started:
match "/restaurants/near/:lat/:lng(/:range)", :to => "restaurants#near", :as => "near", :constraints => {:range => /\d+/}
It seems the router has problems with float parameters, an url like /restaurants/near/53.0123/10.5678 isn't recognized. Do you have a solution or best practice for handling GPS coordinates in rails urls?
Thank you!
The problem is caused because Rails try to use the "dots" for search for the format (.:format)
So, you can add some constraints to fix it, for example:
match "/restaurants/near/:lat/:lng(/:range)", :to => "restaurants#near", :as => "near", :constraints => {:lat => /\-?\d+(.\d+)?/, :lng => /\-?\d+(.\d+)?/ , :range => /\d+/}

problem in routes

i want to change the default route in RoR to what i want:
consider the following example...
:controller/:action/:id
which will give you the route in the browser as:
http://localhost:3000/controller/action/id
now i want to change it to...
http://localhost:3000/this-is-what-i-want/id
we can get an alias for the controller and the action as well like...
resources :controller, :as => "my-custom-name"
and if you want to have the alias for the action, then
resources :controller, :path_names => { :action => 'my-custome-name-1', :action => 'my-custome-name-2' }
BUT i want to change the controller and the action at once... if u noticed the above http://localhost:3000/this-is-what-i-want/id path in the question...
need help...
thanks in advance...
You need a named route.
In Rails2:
map.a_name 'this-is-what-i-want/:id', :controller => 'controller_name', :action => 'action_name'
In Rails3:
match 'this-is-what-i-want/:id' => 'controller_name#action_name'
You want to be using Rest routes, rather than controller/action
I'm going to use "balls" instead of "this-is-what-i-want"
resources :balls
Then, when you link to a ball, do link_to(ball.name, ball).
This will give you a link of http://localhost:3000/balls/45
This rails rest cheatsheet is a good start.

How does path_prefix and requirements in Rails routes work?

Taking the 2 examples below - how do they work?
map.resources :api_developers, :path_prefix => '-'
map.connect '-/:controller/:action/:id', :requirements => { :controller => /metrics|labs/ }
The :path_prefix option lets you add additional parameters that will be prefixed to the recognized paths. For example, suppose each photo in your application belongs to a particular photographer. In that case, you might declare this route:
map.resources :photos, :path_prefix => '/photographers/:photographer_id'
Routes recognized by this entry would include:
/photographers/1/photos/2
/photographers/1/photos
So yours samples
first one
/-/api_developers/
/-/api_developers/1
/-/api_developers/1/edit
etc
second one
/-/metrics/:action/:id
/-/labs/:action/:id
since there is no requirements on :action and :id they can be any string like
/-/metrics/first_string/second_string

Resources