【发布时间】:2010-11-26 15:29:13
【问题描述】:
对于 Commentable 实现,我有一个类似多态的关联(不是真正的 Rails 关联)。不过,我希望能够对所有 cmets 使用相同的视图。对于我命名的路由,我只想能够调用edit_comment_path 并让它转到我的新方法。
我的路线将如下所示:
resources :posts do
resources :comments
end
resources :pictures do
resources :comments
end
resources :comments
现在我已经在一个辅助模块中覆盖了edit_comment_path,但是resources :comments 生成的那个却一直被调用。我保留了resources :comments,因为我希望能够直接访问 cmets 和一些我依赖的 Mixins。
这是我在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
编辑
# take something like [:main_site, @commentable, @whatever] and convert it to "main_site_coupon_whatever"
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
【问题讨论】:
标签: ruby-on-rails routing