Ruby on rails 3 Rails充当可注释的线程和嵌套路由
我有以下路线:Ruby on rails 3 Rails充当可注释的线程和嵌套路由,ruby-on-rails-3,routes,nested-routes,acts-as-commentable,Ruby On Rails 3,Routes,Nested Routes,Acts As Commentable,我有以下路线: resources :organisations do resources :comments, only: [:create, :destroy] resources :events do resources :comments, only [:create, :destroy] end end 正如AACWT示例的文档所示,我尝试在view/comments/_comments.html.erb中使用以下代码为事件添加注释: <%= form_fo
resources :organisations do
resources :comments, only: [:create, :destroy]
resources :events do
resources :comments, only [:create, :destroy]
end
end
正如AACWT示例的文档所示,我尝试在view/comments/_comments.html.erb
中使用以下代码为事件添加注释:
<%= form_for [commentable, Comment.new], :remote => false do |f|%>
def create
@commentable = Event.find(params[:commentable][:commentable_id])
@user_who_commented = current_user
@comment = Comment.build_from(@commentable, @user_who_commented.id, "Comment!" )
end
其中,@事件
在相应的控制器动作中分配
comments\u controller.rb中的create
方法:
<%= form_for [commentable, Comment.new], :remote => false do |f|%>
def create
@commentable = Event.find(params[:commentable][:commentable_id])
@user_who_commented = current_user
@comment = Comment.build_from(@commentable, @user_who_commented.id, "Comment!" )
end
虽然这在此时可能不正确,因为我已经尝试了许多不同的方法来实现这一点,并且需要回到代码产生问题的地方。问题是它试图加载可注释的路径,在本例中为事件,因此它尝试的是event\u path
,而它应该是organization\u event\u path
。如何将其传递给方法
我的想法可能是由一个proc、proc.new |组织、事件|
或类似的东西引起的,但这就是我陷入困境的地方。我还在学习Rails以及ActiveRecord中的关联是如何工作的。任何指针都会得到极大的赞赏,因为它将避免我必须对结构进行大量更改,并将事件作为自己的资源,而不是嵌套,这将产生各种其他问题
这就是说,我在追求实现这一目标的最佳实践方式,所以如果这确实需要改变方案,那么我想我必须这样做