使用commontator gem进行注释时出现“未定义的局部变量或方法”错误
我创建了一个人们可以共享照片的网站(在我的代码中称为引脚)。 我想添加一个系统,当有人点击图片时,他们可以对其进行评论。 我决定使用commontator gem并安装它。 我的问题是评论系统没有像它应该的那样显示在post下面,我得到了一个未定义的局部变量或方法错误我的引脚控制器。
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
pin.rb
class Pin { :medium => "300x300>", :thumb => "100x100>" } validates :image, presence: true acts_as_commontator acts_as_commontable end
pins_controller.rb
class PinsController 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 #<#:0x007f9d8df68768> Extracted source (around line #12):
****
由于没有添加堆栈跟踪,因此有几点意见。
-
acts_as_commontator
和acts_as_commontable
添加在同一模型中。
根据https://github.com/lml/commontator上的文档
-
acts_as_commontator
//将添加到用户模型(或任何应该能够发表评论的模型)中
-
acts_as_commontable
//将添加到您希望能够评论的模型中
那么你可以尝试将acts_as_commontator
移动到用户模型吗?
-
在pin.rb
行中没有。 3 ,。删除你的gem commontator没有使用的行act_as_commentable
假设您已将acts_as_commontable
添加到Pin
模型,
在pins/show.html.erb
,
更换
<%= commontator_thread(commontable) %>
同
<%= commontator_thread(@pin) %>
根据Commontator使用文档 ,
在<%= commontator_thread(commontable) %>
commontable
是acts_as_commontable
的模型实例。
在你的情况下是@pin
。