[英]Polymorphic comments with nested resources in Ruby on Rails
I've followed the Go Rails tutorial below to set up comments with polymorphic associations: https://gorails.com/episodes/comments-with-polymorphic-associations我已经按照下面的 Go Rails 教程设置了具有多态关联的注释: https://gorails.com/episodes/comments-with-polymorphic-associations
However, I have a nested situation with two models (movies/parts).但是,我有两个模型(电影/部分)的嵌套情况。 It is working with 'movies' but I cannot get it working with child model 'parts'.
它正在与“电影”一起使用,但我无法与孩子 model 的“零件”一起使用。
Models楷模
class Comment < ApplicationRecord
belongs_to :user
belongs_to :commentable, polymorphic: true
end
class Movie < ApplicationRecord
has_many :parts, dependent: :destroy
has_many :comments, as: :commentable
end
class Part < ApplicationRecord
belongs_to :movie
has_many :comments, as: :commentable
end
config/routes.rb配置/路由.rb
resources :movies do
resources :comments, module: :movies
resources :parts do
resources :comments, module: :parts
end
end
app/views/movies/show.html.erb应用程序/视图/电影/show.html.erb
<%= render partial: "comments/comments", locals: {commentable: @movie} %>
<%= render partial: "comments/form", locals: {commentable: @movie} %>
app/views/comments/_comments.html.erb应用程序/views/comments/_comments.html.erb
<h1>Comments</h1>
<% commentable.comments.each do |comment| %>
<div class="well">
<%= comment.summary %> by <i><%= comment.user.email %></i><br><br>
</div>
<% end %>
app/views/comments/_form.html.erb app/views/comments/_form.html.erb
<%= form_for [commentable, Comment.new] do |form| %>
<% if commentable.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(commentable.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% commentable.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= form.text_area :summary, class: "form-control", placeholder: "Add a comment" %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Everything above with 'movies' is working well.上面带有“电影”的所有内容都运行良好。 The problem is with 'parts'.
问题出在“零件”上。
app/views/parts/show.html.erb应用程序/视图/零件/show.html.erb
<%= render partial: "comments/comments", locals: {commentable: @part} %>
<%= render partial: "comments/form", locals: {commentable: @part} %>
Error with 'parts' “零件”错误
NoMethodError in Parts#show undefined method `part_comments_path' for #ActionView::Base:0x0000000003d3b0 NoMethodError in Parts#show undefined method `part_comments_path' for #ActionView::Base:0x0000000003d3b0
Highlighted line of error:突出显示的错误行:
<%= form_for [commentable, Comment.new] do |form| %>
I think I have to pass the movie object with the part object into 'commentable' -- since it is nested -- but don't know how to do it with this set up.我想我必须通过电影 object 和 object 部分进入“可评论”——因为它是嵌套的——但不知道如何使用这个设置。 Any suggestions are greatly appreciated.
非常感谢任何建议。
I believe route in the partial meant replacing any references to part_comments_path with movie_part_comments_path.我相信部分路径意味着用movie_part_comments_path替换对part_comments_path的任何引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.