Reputation: 359
baby Ruby coder here.
I followed the steps here:https://masteruby.github.io/weekly-rails/2014/08/05/how-to-add-voting-to-rails-app.html#.XMFebOhKg2w to upload this act_as_votable gem however when I refresh my site to see if it works I get the following error: undefined method `acts_as_votable' for #<Class:0x00007f65df881b38>
The code does not seem to like the fact that I have put act_as_votable
in my model I would like to use it on.
The error in my console also indicates that something is wrong in my controller. Do I need to define something there too?
Thanks in advance,
Angela
Model I want to use the act_as_votable gem on, you can see i have added it as the instructions instructed:
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
My hairstyles controller with the 'upvote' method at the end:
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
My index file i'd like to display the gem on:
<% @hairstyles.each do |hairstyle| %>
<%= link_to "upvote", like_hairstyle_path(hairstyle), method: :put%>
<%end %>
</div>
Here is my repo if needed :https://github.com/Angela-Inniss/hair-do
Upvotes: 2
Views: 80
Reputation: 3662
I cloned & setup your repo & I saw a few things going on that might be the cause:
act_as_taggable
on your models. Your models should be annotated as acts_as_taggable
(plural)spring stop
and trying to start your server again (I disable spring on all of my rails projects)Upvotes: 0
Reputation: 6445
It looks like you didn't run all 4 setup steps:
add 'acts_as_votable' to gemfile
Then run from terminal:
bundle install
rails generate acts_as_votable:migration
rake db:migrate
Upvotes: 1