I have a polymorphic-like association (and not a true Rails-one) to implement Commentable. However, I would like to use the same views for all comments. For my named routes, I just want to call edit_comment_pathand go to my new method.
My routes will look something like this:
resources :posts do
resources :comments
end
resources :pictures do
resources :comments
end
resources :comments
I am currently overridden edit_comment_pathin a helper module, but the one that is generated resources :commentscontinues to receive the call. I save resources :commentsbecause I would like to have access to comments directly, and some of them rely on him.
Here is my override method in module CommentsHelper:
def edit_comment_path(klass = nil)
klass = @commentable if klass.nil?
if klass.nil?
super
else
_method = "edit_#{build_named_route_path(klass)}_comment_path".to_sym
send _method
end
EDIT
def build_named_route_path(args)
args = [args] if not args.is_a?(Array)
path = []
args.each do |arg|
if arg.is_a?(Symbol)
path << arg.to_s
else
path << arg.class.name.underscore
end
end
path.join("_")
end