ArgumentError:make_flaggable gemを使用してマイクロポストにいいね、お気に入り、不適切なボタンを追加するときに、引数の数が間違っています(0の場合は3)

タコア

マイクロポストに「いいね」、「お気に入り」、「不適切」なボタンを追加したい。

ユーザーはマイクロポストを1回だけ好きになれます。ただし、マイクロポストが好きなユーザーは、お気に入りのボタンをクリックすることもできます。

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
imechemi

まず第一に、ここmake_flaggableのソースコードに記載されているように、メソッドはパラメータを受け入れません。そのため、0引数エラーに対して3が表示されます。

make_flaggableモデル内で引数を渡す必要はありません

micropost.rb

make_flaggable

デフォルトでは、モデルは他のモデルによってフラグが立てられるようになります。

フラガーとしてマークされているモデルでフラグを立てられるようにしたい場合。次に、フラガーとしてマークされているモデルによってのみマイクロポストにフラグが立てられるように制限できます。

micropost.rb

make_flaggable :once_per_flagger => true

したがって、モデルの場合、パラメータをに渡さないでくださいmake_flaggable

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ