使用commontator gem进行注释时出现“未定义的局部变量或方法”错误

时间:2014-03-12 05:10:53

标签: ruby-on-rails ruby-on-rails-4 commenting

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

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

<%= 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

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

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 %>

2 个答案:

答案 0 :(得分:3)

由于没有添加堆栈跟踪,因此有几点意见。

  1. acts_as_commontatoracts_as_commontable已添加到同一模型中。

    根据https://github.com/lml/commontator

    的文件
    • acts_as_commontator //将添加到用户模型(或任何应该能够发表评论的模型)
    • acts_as_commontable //将添加到您希望能够发表评论的模型中

      您可以尝试将acts_as_commontator移至用户模型吗?

  2. pin.rb行号3,。删除宝石commontator未使用的行acts_as_commentable

答案 1 :(得分:2)

假设您已将acts_as_commontable添加到Pin模型,

pins/show.html.erb

替换

<%= commontator_thread(commontable) %>

使用

<%= commontator_thread(@pin) %> 

根据 Commontator Usage Documentation

<%= commontator_thread(commontable) %>

commontableacts_as_commontable

的模型实例

在您的情况下为@pin