有谁知道在 Sinatra 中创建投票系统有什么用 gem 吗?

Does anyone know any useful gem to create voting system in Sinatra?

您好,我正在创建一个小型 Sinatra 应用程序并尝试在其中创建一个投票系统。

我尝试使用 acts_as_votable gem 但它给我一些错误。

rake db:migrate                                                                                                                                  
rake aborted!                                                                                                                                         
TypeError: ActsAsVotable is not a class

这是我尝试从 gem 的源代码制作的迁移文件。 class ActsAsVotable < ActiveRecord::Migration[5.1] 定义 self.up create_table :投票 |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 的任何其他 alternet 解决方案吗?

正如您在 the "Acts As Votable" documentation 中看到的那样,它是 gem 与 Rails 框架集成的,所以我不敢打赌它能与 Sinatra 一起正常工作(即使您使用 ActiveRecord迁移)。

最后你可以转Rails或者自己写投票代码;您可以查看 Gerry 发布的示例或尝试“Create your first voting app with Sinatra”。