Rails嵌套模型,注释为可注释(多态关联)

时间:2016-09-07 23:33:47

标签: ruby-on-rails comments ruby-on-rails-5

我有问题和回答模型,评论两者都是可评论的。关于这个问题的评论工作正常,但我得到的答案是:“未定义的方法`评论'为nil:NilClass。”以下是代码:

控制器/ comment_controller.rb

class CommentsController < ApplicationController      
  before_action :authenticate_user!    
  def create
    @comment = @commentable.comments.new comment_params        
    @comment.user = current_user        
    @comment.save
    redirect_to @commentable, notice: "Thanks for your comment."    
  end      
  private
    def comment_params
      params.require(:comment).permit(:content)
    end
end

控制器/问题/ comment_controller.rb

class Questions::CommentsController < CommentsController
  before_action :set_commentable      
  private      
    def set_commentable
      @commentable = Question.find_by_permalink(params[:question_id])
    end      
end 

控制器/问题/答案/ comment_controller.rb

class Answers::CommentsController < CommentsController      
  before_action :set_commentable      
  private      
    def set_commentable
      @question = Question.find_by_permalink(params[:question_id])
      @commentable = @question.answers.find(params[:answer_id])       
    end      
end

模型

class Question < ApplicationRecord
  belongs_to :user
  has_many :answers,    dependent: :destroy
  has_many :comments,   as: :commentable, dependent: :destroy
end

class Answer < ApplicationRecord  
  belongs_to :user  
  belongs_to :question
  has_many :comments,   as: :commentable, dependent: :destroy 
end

class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
  belongs_to :user
end

路线:

resources :questions do
    resources :comments, module: :questions 
      resources :answers, :only => [:create, :destroy, :update] do
        resources :comments, module: :answers
    end end

问题:show.html.erb:

<h1><%= link_to @question.title, question_path(@question)%></h1>
<p><%= @question.description %></p>
<%= render partial: "comments/comments", locals: {commentable: @question } %>
<%= render partial: "comments/form", locals: {commentable: @question } %>

<% @question.answers.each do |answer| %>
 <%= render partial: "comments/comments", locals: {commentable: @answer } %>
 <%= render partial: "comments/form", locals: {commentable: @answer } %>
<% end %>

_comments.html.erb

    <div class="content">
   <h2 class="title"><%= pluralize(commentable.comments.count, 'Comment', plural: 'Comments') %></h2>           
        <% commentable.comments.each do |comment| %>             
            <small><%= time_ago_in_words(comment.created_at) %></small> 
            <p><%= comment.content %></p>
        <% end %>
    </div>

_form.html.erb

<%= form_for [commentable, Comment.new] do |f| %>
    <%= f.text_area :content, class:'textarea', placeholder:'Add a comment' %>
    <%= f.submit 'Comment', class:'button is-primary' %>
<% end %>

更新 发生了奇怪的事情。我重置数据库和替换我现在可以添加评论答案,不知道它是如何修复的。 但是我得到一个更有意义的新错误:#&lt;#:0x007f89e04905b8&gt;的未定义方法`answer_comments_path'。我把&lt;%= answer.inspect%&gt;就在评论输入表格到答案之前,我得到了预期的答案模型数据。

所以当我做rake路线时,我看不到answer_comments_path。 路线是question_answer_comments_path。如何将此路径传递给答案/评论表单。

0 个答案:

没有答案