before_action :set_post
def create
current_member.likes.create(post: @post)
render partial: 'shared/posts/liking', object: @post.reload, as: :post
end
def destroy
current_member.likes.find_by(post: @post).destroy
render partial: 'shared/posts/liking', object: @post.reload, as: :post
end
<div class="flex items-center space-x-1">
<% if member_signed_in? %>
<% unless post.liked_by?(current_member) %>
<!-- like -->
<button type="button"
class="flex items-center"
hx-post="<%= like_post_path(post) %>"
hx-target="closest div"
hx-swap="outerHTML"
title="Like">
<%= heroicon "thumb-up", variant: :outline, options: { class: "w-6 h-6" } %>
</button>
<% else %>
<!-- unlike -->
<button type="button"
class="flex items-center"
hx-delete="<%= unlike_post_path(post) %>"
hx-target="closest div"
hx-swap="outerHTML"
title="Unike">
<%= heroicon "thumb-up", variant: :solid, options: { class: "w-6 h-6" } %>
</button>
<% end %>
<% else %>
<!-- sign in -->
<%= link_to new_member_session_path, title: 'Like' do %>
<%= heroicon "thumb-up", variant: :outline, options: { class: "w-6 h-6" } %>
<% end %>
<% end %>
</div>
post 'likes/:post_id', to: 'likes#create', as: :like_post
delete 'likes/:post_id', to: 'likes#destroy', as: :unlike_post
Martin Sojka, Maker of CodeSnips