Ruby on rails '中未定义的方法;注释路径';使用;“可评论的”;多态关联的形式_

Ruby on rails '中未定义的方法;注释路径';使用;“可评论的”;多态关联的形式_,ruby-on-rails,ruby,ruby-on-rails-4,polymorphic-associations,Ruby On Rails,Ruby,Ruby On Rails 4,Polymorphic Associations,我收到一个错误,题为“未定义的方法‘注释\路径’…” 在app/views/comments/_form.html.erb(第1行)中的代码处 最近,我尝试在我正在构建的网站上通过多态关联实现嵌套评论;然而,我遇到了一个无法让我前进的问题 我试图在“可评论”模型(即本例中的博客)上实现嵌套评论,然后单击show时,所有嵌套评论都会显示出来,个人可以在博客上发表评论或回复评论(从而产生嵌套评论),而无需离开页面 我已经包括了一些其他文件来显示设置,但是如果我遗漏了一个必要的文件,请让我知道,我会立

我收到一个错误,题为“未定义的方法‘注释\路径’…” 在app/views/comments/_form.html.erb(第1行)中的代码处

最近,我尝试在我正在构建的网站上通过多态关联实现嵌套评论;然而,我遇到了一个无法让我前进的问题

我试图在“可评论”模型(即本例中的博客)上实现嵌套评论,然后单击show时,所有嵌套评论都会显示出来,个人可以在博客上发表评论或回复评论(从而产生嵌套评论),而无需离开页面

我已经包括了一些其他文件来显示设置,但是如果我遗漏了一个必要的文件,请让我知道,我会立即添加它。非常感谢您的帮助。我被难住了好几个小时,我相信这很简单

<%= form_for [@commentable, @comment] do |f| %>
  <%= f.hidden_field :parent_id %></br>
    <%= f.label :content, "New Comment" %></br>
    <%= f.text_area :body, :rows => 4 %></br>
    <%= f.submit "Submit Comment" %>
<% end %>
博客和评论模型

class Blog < ActiveRecord::Base
  validates_presence_of :body, :title
  has_many :comments, :as => :commentable, :dependent => :destroy
  mount_uploader :image, ImageUploader
end

class Comment < ActiveRecord::Base
  has_ancestry
  belongs_to :commentable, :polymorphic => true
  validates_presence_of :body
end
@博客

@_params实例变量存在更好的错误

{"utf8"=>"✓", "authenticity_token"=>"OH2tDdI5Kp54hf5J78wXHe//Zsu+0jyeXuG27v1REqjdAec7yBdlrVPLTZKEbLZxgR2L7rGwUwz5BlGTnPcLWg==", "comment"=>{"parent_id"=>"", "body"=>"Hello!\r\n"}, "commit"=>"Submit Comment", "controller"=>"comments", "action"=>"create", "blog_id"=>"8"}

此视图中出现错误:
app/views/blogs/show.html.erb

此视图的数据已在
博客#show
中准备好。因此,在您看来,您应该有
,因为您设置了
@blog
,而不是
@commentable

您还应该执行
@comment=comment.new
。不知道你把这个放在哪里


在视图(app/views/blogs/show.html.erb)中执行
。如果是nil,那么这就是为什么会出现错误。

检查@commentable是否存在。如果为nil,则会出现此错误。@Swards-commentable作为实例变量存在,并且与“blog”实例变量的内容完全相同。。。实例变量'comments'显示{},实例变量'comment'显示“#”
[@commentable,@comment]
,如果@commentable是blog实例,则表单中的将调用
blog\u comments\u path
方法。您检查了表单中的@commentable变量吗?(“雇员再培训局中的raise@commentable”)@Swards-Gotcha。在更好的错误中,当从完整的博客列表中单击博客时,它会引发未定义的方法错误,它会显示以下内容。。(我将在上面添加它。)我不确定您在表单中检查commentable变量并在erb中提出commentable是什么意思。请解释一下?谢谢由于您使用了更好的错误,请将
添加到
表单\u之前。然后,您可以检查所有实例变量。因此,可注释实例变量在我的可注释模块中设置,设置本身等于您尝试访问的当前控制器和ID(在find_commentable下的comments_controller中单击create时调用)(实际上,这样您就可以将模块添加到任何您想评论的内容中,而不是再次编写所有代码?).我是新来的,所以我问这个,不说。问题出在我的可评论模块中,你可以看到难以捉摸的大写字母C。我把它改成小写,现在我可以查看博客了。但是…@Swards-我现在在我的评论中的第12行出现了一个禁止的属性错误。\u controller。我想我把我的属性设置为co在def comment_params下的private部分的controller中更进一步?也许我错过了创建操作所必需的一个?我编辑了原始帖子,为params实例变量输入了更好的错误输出,如果这能为您提供见解的话。再次感谢所有的帮助,并对我缺乏知识表示歉意。您的
 comment_params
方法看起来不错,只是您没有使用它;)它应该是
@comment=@commentable.comments.build(comment_params)
class BlogsController < ApplicationController
  before_filter :authenticate, :except => [ :index, :show ]
  before_action :set_blog, only: [:show, :edit, :update, :destroy]
  include MyModules::Commentable

  # GET /blogs
  # GET /blogs.json
  def index
    @blogs = Blog.all.order('created_at DESC')

    respond_to do |format|
      format.html 
      format.json
      format.atom
    end
  end

  # GET /blogs/1
  # GET /blogs/1.json
  def show
    @blog = Blog.find(params[:id])
  end

  # GET /blogs/new
  def new
    @blog = Blog.new
  end

  # GET /blogs/1/edit
  def edit
    @blog = Blog.find(params[:id])
  end

  # POST /blogs
  # POST /blogs.json
  def create
    @blog = Blog.new(blog_params)

    respond_to do |format|
      if @blog.save
        flash[:success] = "Blog was sucessfuly created"
        format.html { redirect_to @blog }
        format.json { render :show, status: :created, location: @blog }
      else
        format.html { render :new }
        format.json { render json: @blog.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /blogs/1
  # PATCH/PUT /blogs/1.json
  def update
    respond_to do |format|
      if @blog.update(blog_params)
        flash[:success] = "Blog was successfully updated."
        format.html { redirect_to @blog }
        format.json { render :show, status: :ok, location: @blog }
      else
        format.html { render :edit }
        format.json { render json: @blog.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /blogs/1
  # DELETE /blogs/1.json
  def destroy
    @blog.destroy
    respond_to do |format|
      flash[:success] = "Blog was successfully deleted."
      format.html { redirect_to blogs_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_blog
      @blog = Blog.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def blog_params
      params.require(:blog).permit(:title, :body, :image, :image_cache, :remote_image_url)
    end

  private
  def authenticate
    authenticate_or_request_with_http_basic do |name, password|
      name == "admin" && password == "runfast"
    end
  end
end
Rails.application.routes.draw do
  resources :blogs do
    resources :comments
  end

  resources :applications

  resources :reviews

  resources :properties

  root to: 'blogs#index'
end
class Blog < ActiveRecord::Base
  validates_presence_of :body, :title
  has_many :comments, :as => :commentable, :dependent => :destroy
  mount_uploader :image, ImageUploader
end

class Comment < ActiveRecord::Base
  has_ancestry
  belongs_to :commentable, :polymorphic => true
  validates_presence_of :body
end
require 'active_support/concern'

module MyModules::Commentable
    extend ActiveSupport::Concern

    included do 
      before_filter :comments, :only => [:show]
    end

    def comments
      @Commentable = find_commentable
      @comments = @Commentable.comments.arrange(:order => :created_at)
      @comment = Comment.new
    end

    private

    def find_commentable
      return params[:controller].singularize.classify.constantize.find(params[:id])
    end

  end
#<Blog id: 8, title: "New Blog Post about Databases.... Again", body: "RDM, the database management system, was designed ...", created_at: "2015-03-01 22:28:07", updated_at: "2015-03-03 00:11:07", image: "IMG_2210.JPG">
#<Blog id: 8, title: "New Blog Post about Databases.... Again", body: "RDM, the database management system, was designed ...", created_at: "2015-03-01 22:28:07", updated_at: "2015-03-03 00:11:07", image: "IMG_2210.JPG">
module CommentsHelper
  def nested_comments(comments)
    comments.map do |comment, sub_comments|
      content_tag(:div, render(comment), :class => "media")
    end.join.html_safe
  end
end
{"utf8"=>"✓", "authenticity_token"=>"OH2tDdI5Kp54hf5J78wXHe//Zsu+0jyeXuG27v1REqjdAec7yBdlrVPLTZKEbLZxgR2L7rGwUwz5BlGTnPcLWg==", "comment"=>{"parent_id"=>"", "body"=>"Hello!\r\n"}, "commit"=>"Submit Comment", "controller"=>"comments", "action"=>"create", "blog_id"=>"8"}