1

ユーザーがマイクロポストに投票できるようにするために、thumbsup gem を使用しています。現在、選択したマイクロポストに投票したすべてのユーザーをレンダリングしようとしています。投票機能とルートはすべて正常に動作しますが、Micropost コントローラーからエラーが発生しています。

undefined method `voted_for?'

マイクロポスト コントローラー:

def into_it  #for the view; displays who likes the post
  @micropost = Micropost.find(params[:id])
  @users = User.voted_for?(@micropost)
  render 'show_users_into_it'
end

マイクロポスト モデル:

acts_as_voteable

ユーザーモデル:

acts_as_voter

スキーマ情報:

# Table name: users
#  id                       
#  name            
#  email    

# Table name: microposts
#  id         :integer       
#  comment    :text
#  user_id    :integer    

# Table name: votes
#  id            :integer          not null, primary key
#  vote          :boolean          default(FALSE), not null
#  voteable_id   :integer          not null
#  voteable_type :string(255)      not null
#  voter_id      :integer
#  voter_type    :string(255)

簡単な方法がない代わりに、ある種の SQL クエリを実行する必要がありますか? ありがとう。

4

2 に答える 2

2

voted_for?ユーザーのインスタンスではなく User クラスで呼び出しているため、未定義のメソッドを取得しています。

あなたが探している方法は、次のように使用されるvoters_who_votedだと思います:

def into_it  #for the view; displays who likes the post
  @micropost = Micropost.find(params[:id])
  @users = @micropost.voters_who_voted
  render 'show_users_into_it'
end
于 2013-01-09T02:24:31.643 に答える
-1

これは、 @users = User.voted_for?(@micropost) が true または false を返しますが、インスタンス オブジェクトは返さないためです。このリンクを見てみてくださいhttp://openrails.blogspot.com/2012/01/thumbs-up-in-rails.html

于 2013-01-09T02:00:29.243 に答える