“未定义的局部变量或方法”;使用commontator gem发表评论时出错 [英] "Undefined local variable or method" error when using commontator gem for comments

查看:116
本文介绍了“未定义的局部变量或方法”;使用commontator gem发表评论时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个网站,人们可以在其中共享照片(在我的代码中被称为图钉)。我想添加一个系统,当有人单击图片时,他们可以对其进行评论。我决定使用 commontator gem 并安装了它。我的问题是,评论系统没有像预期的那样显示在帖子下方,并且我的pin控制器收到未定义的局部变量或方法错误。

I made a website in which people can share photo's (they are called pins in my code). I wanted to add a system in which, when someone clicks on the picture, they can comment on it. I decided to use the commontator gem and I installed it. My problem is that the commenting system does not show up below posts like it's supposed to and I get an undefined local variable or method error for my pins controller.

routes.rb

routes.rb

Photo::Application.routes.draw do
resources :pins

devise_for :users
root "pins#index"
get "about" => "pages#about"
mount Commontator::Engine => '/commontator'

show.html.erb

show.html.erb

<%= link_to 'Back', pins_path %>
<div class="row">
  <div class="col-md-offset-2 col-md-8">
    <div class="panel panel-default">
      <div class="panel-heading center">
        <%= image_tag @pin.image.url(:medium) %>
      </div>
      <div class="panel-body">
      <p><%= @pin.description %></p>
      <p><strong><%= @pin.user.name if @pin.user %></strong></p>
      <%= commontator_thread(commontable) %>

      <% if @pin.user == current_user %>
          <%= link_to edit_pin_path(@pin) do %>
              <span class="glyphicon glyphicon-edit"></span>            
          <% end %>
      <% end %>
    </div>
  </div>
</div>

pin.rb

pin.rb

class Pin < ActiveRecord::Base
    belongs_to :user
    acts_as_commentable

    has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }

    validates :image, presence: true
    acts_as_commontator
    acts_as_commontable
end

pins_controller.rb

pins_controller.rb

class PinsController < ApplicationController
    before_action :set_pin, only: [:show, :edit, :update, :destroy]
    before_action :correct_user, only: [:edit, :update, :destroy]
    before_action :authenticate_user!, except: [:index, :show]

    def index
       @pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 8)
    end

    def show
    end

    def new
        @pin = current_user.pins.build
    end

    def edit
    end

    def create
        @pin = current_user.pins.build(pin_params)
        if @pin.save
          redirect_to @pin, notice: 'Pin was successfully created.'
        else
          render action: 'new'
        end
    end

    def update
        if @pin.update(pin_params)
            redirect_to @pin, notice: 'Pin was successfully updated.'
        else
           render action: 'edit'
        end
    end

    def destroy
        @pin.destroy
        redirect_to pins_url
    end

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

    def correct_user
        @pin = current_user.pins.find_by(id: params[:id])
        redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
    end

    def pin_params
      params.require(:pin).permit(:description, :image)
    end
end

用户模型 user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

    has_many :pins, dependent: :destroy 
validates :name, presence: true
acts_as_commontator
end

错误我遇到了 show.html.erb

NameError in Pins#show
undefined local variable or method `commontable' for #<#<Class:0x007f9d8ccec328>:0x007f9d8df68768>
Extracted source (around line #12):

                   <div class="panel-body">
                   <p><%= @pin.description %></p>
                   <p><strong><%= @pin.user.name if @pin.user %></strong></p>
                               **<%= commontator_thread(commontable) %>**

                   <% if @pin.user == current_user %>
                           <%= link_to edit_pin_path(@pin) do %>


推荐答案

由于未添加stacktrace,因此有几点观察。

As there is no stacktrace added, a couple of observations.


  1. acts_as_commontator acts_as_commontable 被添加到同一模型中。

  1. acts_as_commontator and acts_as_commontable are added in same model.

根据 https://github.com/上的文档lml / commontator


  • acts_as_commontator //在用户模型(或应该能够发表评论的任何模型)中添加的

  • acts_as_commontable //要添加到您希望对其发表评论的模型中

所以您可以尝试移动 acts_as_commontator 到用户模型?

So can you try moving acts_as_commontator to user model?

pin中。 rb 行号3,删除您的gem 未使用的行 acts_as_commentable commonrator

In pin.rb line no. 3,.remove the line acts_as_commentable which is not used by your gem commontator

这篇关于“未定义的局部变量或方法”;使用commontator gem发表评论时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆