婴儿Ruby编码器在这里。
我按照此处的步骤操作:https://masteruby.github.io/weekly-rails/2014/08/05/how-to-add-voting-to-rails-app.html#.XMFebOhKg2w上传此act_as_votable gem,但是当我刷新网站以查看其是否正常工作时,出现以下错误:undefined method `acts_as_votable' for #<Class:0x00007f65df881b38>
该代码似乎不喜欢我在模型中使用act_as_votable
的事实。
控制台中的错误也表明控制器中有问题。我是否也需要在其中定义一些内容?
谢谢,
安吉拉
我想在act_as_votable gem上使用模型,您可以看到我已经按照说明添加了它:
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
acts_as_votable
mount_uploader :photo, PhotoUploader
end
我的发型控制器的末尾带有'upvote'方法:
class HairstylesController < ApplicationController
def index
if params[:category].present?
@hairstyles = Hairstyle.where(category: params[:category])
elsif params[:search].present?
@hairstyles = Hairstyle.where('name ILIKE ?', '%#{params[:search]}%')
else
@hairstyles = Hairstyle.all
end
end
def show
@hairstyle = Hairstyle.find(params[:id])
@comment = Comment.new
end
def new
@hairstyle = Hairstyle.new
end
def create
@hairstyle = Hairstyle.create(hairstyle_params)
@hairstyle.user = current_user
if @hairstyle.save!
redirect_to hairstyle_path(@hairstyle)
else
render 'new'
end
end
def edit
@hairstyle = Hairstyle.find(params[:id])
end
def update
@hairstyle = Hairstyle.find(params[:id])
@hairstyle.update(hairstyle_params)
redirect_to hairstyles_path
end
def destroy
@hairstyle = Hairstyle.find(params[:id])
@hairstyle.destroy
redirect_to hairstyles_path
end
def upvote
@hairstyle = Hairstyle.find(params[:id])
@hairstyle.upvote_by current_user
redirect_to hairstyles_path
end
private
def hairstyle_params
params.require(:hairstyle).permit(:name, :description, :category, :location, :stylist, :photo, :video_url, :photo_cache)
end
end
我的索引文件想在以下位置显示宝石:
<% @hairstyles.each do |hairstyle| %>
<%= link_to "upvote", like_hairstyle_path(hairstyle), method: :put%>
<%end %>
</div>
如果需要,这是我的仓库:https://github.com/Angela-Inniss/hair-do
答案 0 :(得分:1)
您似乎没有执行所有4个设置步骤:
将'acts_as_votable'添加到gemfile
然后从终端运行:
bundle install
rails generate acts_as_votable:migration
rake db:migrate
答案 1 :(得分:0)
我克隆并设置了您的仓库,我发现可能正在发生一些事情:
act_as_taggable
引起的。您的模型应标注为acts_as_taggable
(复数)spring stop
并尝试再次启动服务器(我在所有Rails项目中均禁用spring)