In a Rails 3.1 application, I want to list a bunch of objects of a class of variables (from a polymorphic table) that I donβt know beforehand. For those who have resources with a named route, I would like to use this route in the link_to call. A naive approach without checking if such a route exists (sorry HAML):
%ul - @objects.each do |object| %li= link_to object, url_for(object)
This will undefined method 'foo_path' error if the object is an instance of the Foo class that does not have a named route (for example, because it is not defined as a resource). Is there a simple way (for example, a simple method call) to determine if a named route exists for an object or class?
EDIT:
What I would like to get is something like this:
%ul - @objects.each do |object| %li= link_to_if object.has_route?, object, url_for(object)
source share