gpt4 book ai didi

ruby-on-rails - 干掉几个模型的重复路线

转载 作者:行者123 更新时间:2023-12-04 06:47:49 25 4
gpt4 key购买 nike

我有一些模型,如新闻和下载,其中有_许多评论:as => :commentable。评论belongs_to :commentable 并且是:polymorphic。和评论一样,有订阅。所以我的路线是这样的:

resources :news do
resources :comments do
post :like, :on => :member
delete :like, :on => :member
end
resources :subscriptions
...
end

resources :downloads do
resources :comments do
post :like, :on => :member
delete :like, :on => :member
end
resources :subscriptions
...
end

...

有没有办法把它弄干?我试着定义
def comment_resources
resources :comments do
# ...
end
resources :subscriptions
end

然后在routes.rb的顶部,然后调用comment_resources,但对我来说似乎很脏,因为它没有正确命名空间/在类中。

最佳答案

1)如果你在新闻和下载中有完全相同的内部路线,你可以

%w(news downloads).each do |res|
resources res do
resources :comments do
post :like, :on => :member
delete :like, :on => :member
end
resources :subscriptions
...
end
end

2) 否则你可以看 https://github.com/rails/routing_concerns (将作为 rails-4 的一部分包含在内):
concern :commentable do
resources :comments do
post :like, :on => :member
delete :like, :on => :member
end
resources :subscriptions
end

resources :news, concerns: :commentable do
...
end
resources :downloads, concerns: :commentable do
...
end

关于ruby-on-rails - 干掉几个模型的重复路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3521388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com