i am trying to user acts_as_votable gem to make user vote for stars
this is my stars-controller
class StarsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
before_action :upvote
def index
@stars = Star.all.order("created_at DESC")
end
def upvote
# @star = Star.find(params[:id])
@star.upvote_by current_user
redirect_to @star
end
def show
@star= Star.find(params[:id])
end
def new
end
def create
@star =Star.new(stars_params)
@star.save
redirect_to @star
end
private
def stars_params
params.require(:star).permit(:name , :description , :image)
end
end
and this is my star model
class Star < ActiveRecord::Base
has_many :books
has_attached_file :image, :styles => { :small => "150x150>" }
validates_attachment_presence :image
validates_attachment_size :image, :less_than => 5.megabytes
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png']
acts_as_votable
end
and the routs
Rails.application.routes.draw do
resources :books
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}
resources :stars do
member do
put "like" , to: "stars#upvote"
end
end
root 'stars#index'
end
i get this error "undefined method `upvote_by' for nil:NilClass"
thanks
Your problem is not really related to acts_as_votable
- your instance variable has not been assigned yet, so practically any method would cause this error. You have to un-comment commented line and change:
before_action :upvote
to
before_action :upvote, except: :index
This logic however seems fishy - why would you like a star to be upvoted when user is editing it? In short: please clarify your requirements.