[英]acts_as_votable , undefined method `upvote_by' for nil:NilClass
我正在嘗試使用act_as_votable gem讓用戶投票給明星
這是我的星星控制器
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
這是我的明星模特
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
和潰敗
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
我收到此錯誤“ nil:NilClass的未定義方法`upvote_by'”
謝謝
您的問題實際上與acts_as_votable
無關-您的實例變量尚未分配,因此實際上任何方法都會導致此錯誤。 您必須取消注釋注釋行並更改:
before_action :upvote
至
before_action :upvote, except: :index
但是,這種邏輯看起來有些可疑-為什么用戶要編輯星星時為什么要對其進行投票? 簡而言之:請闡明您的要求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.