Angela Inniss
Angela Inniss

Reputation: 359

added gem and got this error undefined method `acts_as_votable' for #<Class:0x00007f65df881b38>

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

Answers (2)

Jay Dorsey
Jay Dorsey

Reputation: 3662

I cloned & setup your repo & I saw a few things going on that might be the cause:

  • The original error I got in this clone was due to act_as_taggable on your models. Your models should be annotated as acts_as_taggable (plural)
  • When I was initially testing, I wasn't logged in. This silently fails, which makes it seem like upvote isn't work. You might want to disable those hearts unless a user is logged in
  • Your html/erb template has some commented out code and such. This might be causing the link/URL to get swallowed and be non-clickable. I resolved it by deleting all styling & formatting except the link I was testing. I like using Haml to help reduce these kinds of nesting errors
  • I didn't run into your class error, but I would suggest running spring stop and trying to start your server again (I disable spring on all of my rails projects)

Upvotes: 0

Mark
Mark

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

Related Questions