Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Users can like posts feature (wip) #1

Merged
merged 10 commits into from
Mar 4, 2024
4 changes: 4 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module PostsHelper
def liked_by?(post, _user_id)
post.likes.exists?(user_id: current_user.id)
end

def current_user_like(post)
post.likes.find_by(user_id: current_user.id)
fedesapuppo marked this conversation as resolved.
Show resolved Hide resolved
end
end
10 changes: 2 additions & 8 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@
<div class="post">
<h2><%= post.title %></h2>
<p><%= post.content %></p>
<p>This post has <%= post.likes.count %>
<%= pluralize(post.likes.count, "like") %>!
</p>
<% if liked_by?(post, current_user) %>
<%= render "posts/partials/unlike_button", post: @post %>
<% else %>
<%= render "posts/partials/like_button", locals: {post: @post} %>
<% end %>
<p>This post has <%= pluralize(post.likes.count, "like") %>! </p>
<%= render "posts/partials/like_button", post: post %>
fedesapuppo marked this conversation as resolved.
Show resolved Hide resolved
<p>Posted by: <%= post.user.name %></p>
</div>
<% end %>
Expand Down
11 changes: 10 additions & 1 deletion app/views/posts/partials/_like_button.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
<p> like button goes here </p>
<% if liked_by?(post, current_user) %>
<p> <%= form_with model: current_user_like(post), url: post_like_path(post, current_user_like(post)), method: :delete, class: 'like-form' do |f| %>
<%= f.submit 'Unlike', class: 'btn btn-danger unlike-button' %>
<% end %></p>
<% else %>
<p> <%= form_with model: Like.new, url: post_likes_path(post), method: :post, class: 'like-form' do |f| %>
<%= f.hidden_field :post_id, value: post.id %>
<%= f.submit 'Like', class: 'btn btn-primary like-button' %>
<% end %> </p>
<% end %>
3 changes: 0 additions & 3 deletions app/views/posts/partials/_unlike_button.html.erb

This file was deleted.