我是铁杆新手 - 所以任何帮助都会非常感激。我目前正在使用GEM' twitter / activerecord-reputation-system'并遵循rails cast上的rails#364。
我不确定为什么我收到错误,因为我认为has_reputation方法应该已经在GEM中自动定义
我还重启了我的控制台/终端,但仍然遇到同样的错误。任何帮助将不胜感激
我的错误: #undefined方法`has_reputation'对于#
Started GET "/events" for 127.0.0.1 at 2014-08-21 10:37:36 +0100
Processing by EventsController#index as HTML
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Completed 500 Internal Server Error in 9ms
NoMethodError (undefined method `has_reputation' for #<Class:0x007f87cb9efed0>):
app/models/event.rb:5:in `<class:Event>'
app/models/event.rb:1:in `<top (required)>'
app/controllers/events_controller.rb:8:in `index'
Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.6ms)
Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.9ms)
模型
#event model
class Event < ActiveRecord::Base
belongs_to :user
has_many :comments, as: :commentable
has_reputation :votes, source: :user, aggregated_by: :sum
validates :city, :date, :description, :name, :price, :time, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :name, length: { minimum: 5 }
validates :name, length: { maximum: 22 }
validates :user_id, presence: true
end
#user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :events, dependent: :destroy
has_many :comments, as: :commentable, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
validates_presence_of :firstname, :lastname
end
路线
Rails.application.routes.draw do
devise_for :users
get '/about', to: 'static_pages#about'
get '/help', to: 'static_pages#help'
get '/terms', to: 'static_pages#terms'
get '/policy', to: 'static_pages#policy'
get '/contact', to: 'static_pages#contact'
get '/blabber', to: 'static_pages#blabber_public'
resources :users do
member do
get :following, :followers
end
end
resources :events do
resources :comments, only: [:create, :destroy]
member { post :vote }
end
root to: 'home#index', as: 'home'
resources :relationships, only: [:create, :destroy]
end
模式
ActiveRecord::Schema.define(version: 20140820114256) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "comments", force: true do |t|
t.text "content"
t.integer "commentable_id"
t.string "commentable_type"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "comments", ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type", using: :btree
create_table "events", force: true do |t|
t.string "name"
t.text "description"
t.date "date"
t.time "time"
t.text "city"
t.decimal "price", precision: 8, scale: 2
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "relationships", force: true do |t|
t.integer "follower_id"
t.integer "followed_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id", using: :btree
add_index "relationships", ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true, using: :btree
add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id", using: :btree
create_table "rs_evaluations", force: true do |t|
t.string "reputation_name"
t.integer "source_id"
t.string "source_type"
t.integer "target_id"
t.string "target_type"
t.float "value", default: 0.0
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "rs_evaluations", ["reputation_name"], name: "index_rs_evaluations_on_reputation_name", using: :btree
add_index "rs_evaluations", ["source_id", "source_type"], name: "index_rs_evaluations_on_source_id_and_source_type", using: :btree
add_index "rs_evaluations", ["target_id", "target_type"], name: "index_rs_evaluations_on_target_id_and_target_type", using: :btree
create_table "rs_reputation_messages", force: true do |t|
t.integer "sender_id"
t.string "sender_type"
t.integer "receiver_id"
t.float "weight", default: 1.0
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "rs_reputation_messages", ["receiver_id"], name: "index_rs_reputation_messages_on_receiver_id", using: :btree
add_index "rs_reputation_messages", ["sender_id", "sender_type"], name: "index_rs_reputation_messages_on_sender_id_and_sender_type", using: :btree
create_table "rs_reputations", force: true do |t|
t.string "reputation_name"
t.float "value", default: 0.0
t.string "aggregated_by"
t.integer "target_id"
t.string "target_type"
t.boolean "active", default: true
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "rs_reputations", ["reputation_name"], name: "index_rs_reputations_on_reputation_name", using: :btree
add_index "rs_reputations", ["target_id", "target_type"], name: "index_rs_reputations_on_target_id_and_target_type", using: :btree
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "firstname"
t.string "lastname"
t.date "dob"
t.string "gender"
t.text "description"
t.string "role"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
events_controller
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
def index
@events = Event.order(:date)
# @events = current_user.events | displays only events by current user
end
def show
@commentable = @event
@comments = @commentable.comments
@comment = Comment.new
end
def new
@event = Event.new
end
def edit
end
def create
@event = Event.new(event_params)
@event.user = current_user
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
respond_to do |format|
if @event.update(event_params)
format.html { redirect_to @event, notice: 'Event was successfully updated.' }
format.json { render :show, status: :ok, location: @event }
else
format.html { render :edit }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
def destroy
@event.destroy
respond_to do |format|
format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
format.json { head :no_content }
end
end
def vote
value = params[:type] == "up" ? 1 : -1
@event = Event.find(params[:id])
@event.add_or_update_evaluation(:votes, value, current_user)
redirect_to :back, notice: "Thank you for voting"
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def event_params
params.require(:event).permit(:name, :description, :date, :time, :city, :price, :user_id)
end
end
的Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.1'
# Use postgresql as the database for Active Record
gem 'pg'
gem 'ancestry'
gem 'acts_as_votable'
gem 'acts_as_list'
gem 'acts_as_commentable', '3.0.1'
gem 'best_in_place'
gem 'carrierwave', '=0.9.0'
gem 'cancan'
gem 'devise'
gem 'fancybox-rails'
gem 'font-awesome-rails'
gem 'jquery-ui-rails'
gem 'kaminari'
gem 'letter_opener'
gem 'ransack'
# gem 'rmagick'
gem 'thumbs_up'
gem 'simple_form'
gem 'activerecord-reputation-system', github: 'NARKOZ/activerecord-reputation-system'
gem 'quiet_assets', group: :development
etc....
答案 0 :(得分:0)
使用支持Rails 4的最新版本的activerecord-reputation-system gem。
更新您的Gemfile:gem 'activerecord-reputation-system'