ArgumentError: wrong number of arguments (3 for 0) when adding like, favorite and inappropriate button to microposts with make_flaggable gem

ArgumentError: wrong number of arguments (3 for 0) when adding like, favorite and inappropriate button to microposts with make_flaggable gem

我想在微博中添加点赞、收藏和不合适的按钮。

微博用户只能点赞一次。喜欢微博的用户也可以点击收藏按钮。

当我在 rails 控制台中尝试时出现错误。

错误

 irb(main):002:0> micropost=Micropost.first

 ArgumentError: wrong number of arguments (3 for 0)
 from /Users/tanerkoroglu/.bundler/ruby/2.0.0/make_flaggable-99297edddfec/lib/make_flaggable.rb:22:in `make_flaggable'
 from /Users/tanerkoroglu/Desktop/Wishpere2/app/models/micropost.rb:9:in `<class:Micropost>'
from /Users/tanerkoroglu/Desktop/Wishpere2/app/models/micropost.rb:1:in `<top (required)>'
  from /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:457:in `load'

micropost.rb

  make_flaggable :like, :inappropriate, :favorite

user.rb

 make_flagger :flag_once => true

create_make_flaggable_tables.rb

class CreateMakeFlaggableTables < ActiveRecord::Migration
 def self.up
   create_table :flaggings do |t|
     t.string :flaggable_type
     t.integer :flaggable_id
     t.string :flagger_type
     t.integer :flagger_id
     t.text :reason

     t.timestamps
  end

   add_index :flaggings, [:flaggable_type, :flaggable_id]
   add_index :flaggings, [:flagger_type, :flagger_id, :flaggable_type, :flaggable_id], :name => "access_flaggings"
  end

 def self.down
     remove_index :flaggings, :column => [:flaggable_type, :flaggable_id]
     remove_index :flaggings, :name => "access_flaggings"

     drop_table :flaggings
 end
end

create_microposts.rb

class CreateMicroposts < ActiveRecord::Migration
 def change
    create_table :microposts do |t|
     t.text :content
     t.references :user, index: true, foreign_key: true

     t.timestamps null: false
 end
     add_index :microposts, [:user_id, :created_at]
 end
end

add_flaggings_count_to_microposts.rb

 class AddFlaggingsCountToMicroposts < ActiveRecord::Migration
   def change
     add_column :microposts,:flaggings_count, :integer
   end
 end

add_flaggings_count_to_users.rb

  class AddFlaggingsCountToUsers < ActiveRecord::Migration
    def change
       add_column :users, :flaggings_count, :integer
    end
  end

首先,make_flaggable 方法不接受源代码中提到的任何参数 here,这就是为什么您会看到 3 for 0 argument 错误。

您不必在模型中向 make_flaggable 传递任何参数。

micropost.rb

make_flaggable

默认情况下,该模型可以被任何其他模型标记。

如果您希望它被标记为标记者的模型标记。然后你可以限制微博只被标记为flagger的模型标记。

micropost.rb

make_flaggable :once_per_flagger => true

所以对于您的模型,不要将任何参数传递给 make_flaggable