rails 上的嵌套资源和简单资源 ruby 中的多态注释 4
Polymorphic Comments in both nested resource and simple resource ruby on rails 4
我有以下关系路线:
resources :courses, only: [:index, :show] do
resources :enrols, only: [:new, :create]
resources :lectures, only: [:show]
end
resources :code_casts, :path => 'casts', :as => 'casts', only: [:index, :show]
resources :blogs, :path => 'blog', :as => 'blog', only: [:index, :show] do
resources :blog_votes, only: [:create, :destroy]
end
我想要在课程、讲座、code_casts 和博客中使用多态评论。
问题是讲座当然有家长,所以路线将是 course/course_id/lecture/id
,博客路径将是 blog/id
,其中评论将有不同的显示页面。
如果我理解正确的话,深度嵌套的资源并没有什么特别之处。所以你可能需要这样的东西
# routes.rb
concern :commentable do
resources :comments
end
resources :courses, only: [:index, :show] do
resources :enrols, only: [:new, :create]
resources :lectures, only: [:show], concerns: [:commentable]
end
resources :code_casts, :path => 'casts', :as => 'casts', only: [:index, :show]
resources :blogs, :path => 'blog', :as => 'blog', only: [:index, :show], concerns: [:commentable] do
resources :blog_votes, only: [:create, :destroy]
end
这将为讲座和博客创建嵌套的 comments
资源。
比你需要在评论控制器中区分路径
# comments_controller
def create
Comment.create(commentable: commentable, other_params...) # assuming you have `commentable` polymorphic belongs_to
end
# a little more ugly than Ryan suggests
def commentable
if request.path.include?('blog') # be careful. any path with 'blog' in it will match
Blog.find(params[:id])
elsif params[:course_id] && request.path.include?('lecture')
Course.find(params[:course_id).leactures.find(params[:id]) # assuming Course has_many lectures
else
fail 'unnable to determine commentable type'
end
end
所有 'magic' 都在可注释方法中,您将在其中检查路径并确定要选择哪个可注释对象。我使用类似的方法,但是这个确切的代码是在没有测试的情况下凭记忆编写的。希望你已经明白了。
我有以下关系路线:
resources :courses, only: [:index, :show] do
resources :enrols, only: [:new, :create]
resources :lectures, only: [:show]
end
resources :code_casts, :path => 'casts', :as => 'casts', only: [:index, :show]
resources :blogs, :path => 'blog', :as => 'blog', only: [:index, :show] do
resources :blog_votes, only: [:create, :destroy]
end
我想要在课程、讲座、code_casts 和博客中使用多态评论。
问题是讲座当然有家长,所以路线将是 course/course_id/lecture/id
,博客路径将是 blog/id
,其中评论将有不同的显示页面。
如果我理解正确的话,深度嵌套的资源并没有什么特别之处。所以你可能需要这样的东西
# routes.rb
concern :commentable do
resources :comments
end
resources :courses, only: [:index, :show] do
resources :enrols, only: [:new, :create]
resources :lectures, only: [:show], concerns: [:commentable]
end
resources :code_casts, :path => 'casts', :as => 'casts', only: [:index, :show]
resources :blogs, :path => 'blog', :as => 'blog', only: [:index, :show], concerns: [:commentable] do
resources :blog_votes, only: [:create, :destroy]
end
这将为讲座和博客创建嵌套的 comments
资源。
比你需要在评论控制器中区分路径
# comments_controller
def create
Comment.create(commentable: commentable, other_params...) # assuming you have `commentable` polymorphic belongs_to
end
# a little more ugly than Ryan suggests
def commentable
if request.path.include?('blog') # be careful. any path with 'blog' in it will match
Blog.find(params[:id])
elsif params[:course_id] && request.path.include?('lecture')
Course.find(params[:course_id).leactures.find(params[:id]) # assuming Course has_many lectures
else
fail 'unnable to determine commentable type'
end
end
所有 'magic' 都在可注释方法中,您将在其中检查路径并确定要选择哪个可注释对象。我使用类似的方法,但是这个确切的代码是在没有测试的情况下凭记忆编写的。希望你已经明白了。