Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 嵌套资源和简单资源RubyonRails4中的多态注释_Ruby On Rails_Polymorphism_Polymorphic Associations - Fatal编程技术网

Ruby on rails 嵌套资源和简单资源RubyonRails4中的多态注释

Ruby on rails 嵌套资源和简单资源RubyonRails4中的多态注释,ruby-on-rails,polymorphism,polymorphic-associations,Ruby On Rails,Polymorphism,Polymorphic Associations,我有以下关系途径: 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 =

我有以下关系途径:

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
我想在课程、讲座、代码和博客中发表多态性评论


问题是讲座当然有家长,所以路线将是
course/course\u id/teaching/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_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

所有的“魔法”都在可注释的方法中,在该方法中,您将检查路径并确定要选择的可注释对象。我使用类似的方法,但这段代码完全是由内存编写的,不需要测试。希望您已经有了这个想法。

如果我正确理解这个问题,那么深度嵌套的资源没有什么特别之处。所以你可能需要这样的东西

# 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_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

所有的“魔法”都在可注释的方法中,在该方法中,您将检查路径并确定要选择的可注释对象。我使用类似的方法,但这段代码完全是由内存编写的,不需要测试。希望你已经有了这个想法。

那么到底是什么问题呢?您可以在控制器中实现所需的逻辑(要显示其命令)<代码>如果参数[:course_id]…,则将其提取到路由和控制器关注点中。在链接中提供的rails cast中。注释位于相同的资源级别上。我不知道如何将嵌套资源与注释关联起来。那么到底是什么问题呢?您可以在控制器中实现所需的逻辑(要显示其命令)<代码>如果参数[:course_id]…,则将其提取到路由和控制器关注点中。在链接中提供的rails cast中。注释位于相同的资源级别上。我不知道如何将嵌套资源关联到注释。