我想在微帖子中添加喜欢、喜欢和不合适的按钮。
用户只能点赞一次微柱。但喜欢微博客的用户也可以点击最喜欢的按钮。
当我在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
方法不接受源代码中提到的任何参数,这就是为什么您看到3 for 0参数错误的原因。
您不必向模型中的make_flaggable
传递任何参数。
micropost.rb
make_flaggable
默认情况下,任何其他模型都可以标记该模型。
如果你想让它被标记为flagger的模型标记。然后你可以限制微柱只能被标记为flagger的模型标记。
micropost.rb
make_flaggable :once_per_flagger => true
因此,对于您的模型,不要将任何参数传递给make_flaggable
。