我已经安装了act_as_votable gem,并按照说明进行操作,将其添加到我的gemfile中,进行db migration,向模型中添加了“充当votable”,并在控制器中添加了方法。
我还更新了索引页面以显示按钮,但是在加载页面时出现以下错误消息:
undefined local variable or method `act_as_votable' for User (call 'User.connection' to establish a connection):Class
我以前从未见过此错误,我是红宝石的新手...
为便于理解,这是我的存储库:https://github.com/Angela-Inniss/hair-do
用户模型:
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :comments
has_many :hairstyles
has_many :saved_hairstyles
act_as_votable
end
发型模型:
class Hairstyle < ApplicationRecord
belongs_to :user, optional: true
has_many :comments, dependent: :destroy
validates :name, presence: true
validates :description, presence: true
validates :category, presence: true
act_as_votable
mount_uploader :photo, PhotoUploader
end
发型控制器:
def upvote
@hairstyle = Hairstyle.find(params[:id])
@hairstyle.upvote_from current_user
redirect_to hairstyles_path
end
def downvote
@hairstyle = Hairstyle.find(params[:id])
@hairstyle.downvote_from current_user
redirect_to hairstyles_path
end
index.html.erb:
<%= link_to like_hairstyle_path(hairstyle), class: 'like' method: :put do %>
<i class="fa fa-heart">
<span><%= hairstyle.get_upvotes.size %><span>
</i>
<%end %>
<!--this is a link block- create the block and then add elements inside?-->
<%= link_to like_hairstyle_path(hairstyle), class: 'like' method: :put do %>
<i class="fa fa-heart">
<span><%= hairstyle.get_downvotes.size %><span>
</i>
<%end %>
答案 0 :(得分:1)
注意该错误消息是什么?
undefined local variable or method 'act_as_votable' for User (call 'User.connection' to establish a connection):Class
^^^^^^^^^^^^^^
应为:
acts_as_votable
不是:
act_as_votable
(User
和Hairstyle
都错了。)