Rails Routes :requirements - ruby-on-rails

I want to set a route :requirements on an array that verifies a particular parameter is included in an array:
atypes = [:culture, :personality, :communication]
map.with_options(:path_prefix => ':atype',
:requirements => {:atype => atypes.include?(:atype)}) do |assessment|
...
end
I haven't been able to find any documentation on how to accomplish this. Any help would be appreciated.

:requirements option expects regular expression. Something like /(culture|presonality|communication)/. You can also construct one from the array:
atypes = [:culture, :personality, :communication]
map.with_options(:path_prefix => ':atype',
:requirements => /(#{atypes.join('|')})/ ) do |assessment|
...
end

Related

How to add alias in routes.rb

How to add alias to route file?
Something like this:
/rules => 'posts/1', param: id => 1
Is it possible define it in routes.rb
I want
/rules => posts/1
not
/rules/1 => posts/1
Well, let's look at the official Rails routing guide:
You can also define other defaults in a route by supplying a hash for the :defaults option. This even applies to parameters that you do not specify as dynamic segments.
So then:
get "/rules" => "posts#show", :defaults => { :id => "1" }
try this
get '/rules', to: redirect('/posts/1')
http://guides.rubyonrails.org/routing.html#redirection

How to Detect an Integer in Rails 3 Routes?

I would like to do just a little bit of extra logic in rotues.rb, that probably doesn't belong there, but it seems to make the most sense to me.
I have two conflicting routes. To be primitive:
match '/videos/:browseby' => 'videos#browse', :as => "browse_by"
Where :browseby is looking for a string, such as "Tags", to browse videos by tags.
However, (and most probably saw this coming) I also have my basic show resource (again in primitive form):
match '/videos/:id' => 'videos#show', :as => "video"
Where :id is looking for the integer for the video ID.
Is there a way to add a small bit of logic such as...
match '/videos/:id' => 'videos#show', :as => "video", :format(:id) => :integer
(Which is my hypothetical rails syntax, to help show what I'm looking for.)
I know I can munch this in the Controller level, but it makes more sense to me to handle it at the route level.
You could try using :constraints and a regex:
match '/videos/:id' => 'videos#show', :as => "video", :constraints => { :id => /\d/ }
match '/videos/:browseby' => 'videos#browse', :as => "browse_by"
You'll also want to make sure the looser :browseby version comes after the :id version. Note that regex constraints are implicitly anchored at the beginning so that would work as long as your :browseby values didn't start with a number.
If you have tags that do start with numbers then you could use an object for the constraint and then you could include anchors in your regex:
class VideoIdsOnly
def matches?(request)
request.path =~ %r{\A/videos/\d+\z}
end
end
match '/videos/:id' => 'video#show', :as => "video", :constraints => VideoIdsOnly.new
match '/videos/:browseby' => 'videos#browse', :as => "browse_by"

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+/}

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

map.resource, parameter restrictions

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

Resources