您好我正在创建一个小型 Sinatra 应用程序并尝试在其中创建一个投票系统。
我尝试使用acts_as_votable gem,但它给了我一些错误。
rake db:migrate
rake aborted!
TypeError: ActsAsVotable is not a class
这是我尝试从 gem 的源代码制作的迁移文件。类 ActsAsVotable < ActiveRecord::Migration[5.1] def self.up create_table :votes do |t|
t.references :votable, :polymorphic => true
t.references :voter, :polymorphic => true
t.boolean :vote_flag
t.string :vote_scope
t.integer :vote_weight
t.timestamps
end
if ActiveRecord::VERSION::MAJOR < 4
add_index :votes, [:votable_id, :votable_type]
add_index :votes, [:voter_id, :voter_type]
end
add_index :votes, [:voter_id, :voter_type, :vote_scope]
add_index :votes, [:votable_id, :votable_type, :vote_scope]
end
def self.down
drop_table :votes
end
end
我还从 gem 源代码创建了 Acts_as_votable 模块。请看下面的代码
require 'active_record'
require 'active_support/inflector'
$LOAD_PATH.unshift(File.dirname(__FILE__))
module ActsAsVotable
if defined?(ActiveRecord::Base)
require 'acts_as_votable/extenders/votable'
require 'acts_as_votable/extenders/voter'
require 'acts_as_votable/vote'
ActiveRecord::Base.extend ActsAsVotable::Extenders::Votable
ActiveRecord::Base.extend ActsAsVotable::Extenders::Voter
end
end
require 'acts_as_votable/extenders/controller'
ActiveSupport.on_load(:action_controller) do
include ActsAsVotable::Extenders::Controller
end
有什么建议可以使这项工作或 Sinatra 的任何其他替代网络解决方案吗?