开发者

How do I get access to this variable?

开发者 https://www.devze.com 2023-02-17 20:46 出处:网络
Im in the partial _video.html.erb which is in the videos directory of the views directory. I know I have access to the video variable. I have a video_vote model with a has_many/belongs_to association

Im in the partial _video.html.erb which is in the videos directory of the views directory. I know I have access to the video variable. I have a video_vote model with a has_many/belongs_to association with Videos and with Users. I also have implemented a开发者_如何学运维 current_user method. I want to get access to the current user's vote on that video. How do I do it?

This is in the create method of the video_votes controller:

@video = Video.find(params[:video_id])
@vote = current_user.video_votes.find_or_create_by_video_id(@video.id)


Make a function in your user model that is something like:

class User
  # code
  def num_votes_for(video)

    video_votes.find_by_video_id(video.id)
  end
  # more code
end

And in your view, you would do something like

Your number of votes: <%= current_user.num_votes_for(video) %>

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号