###前提・実現したいこと
掲示板のコメントに対するいいね機能を追加しようとしたのですが、
ページを確認したところエラーも出ず、機能が追加されませんでした。
コードを以下のようにしたのですがどのようにしたら、反映されるでしょうか?
###該当のソースコード
comment.rb
has_many :votes, dependent: :destroy has_many :voters, through: :votes, source: :user
user.rb
has_many :votes, dependent: :destroy has_many :voted_comments, through: :votes, source: :comment def votable_for?(comment) comment && comment.user != self && !votes.exists?(comment_id: comment.id) end
vote.rb
belongs_to :comment belongs_to :user validate do unless user && user.votable_for?(comment) errors.add(:base, :invalid) end end
routes.rb
resources :messages do member do ...... ...... end resources :comments, only: [:create, :edit, :update, :destroy] do member { patch "like", "unlike" } collection { get "voted" } end end
comments_controller.rb
def like @comment = Comment.published.find(params[:id]) current_user.voted_comments << @comment redirect_to @comment end
messages/show.html.erb
<% @comments.each do |comment| %> <% if (count = comment.votes.count) > 0 %> | <span class="vote">★<%= count %></span> <% end %> <p class="vote"> <% if current_user && current_user.votable_for?(comment) %> <%= link_to "★good!", [:like, comment], method: :patch, class: "button" %> <% end %
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/09 07:55