Create a toggle button with Hotwire and Rails
If you have a one-off toggle interaction, it's easy to wire up with Turbo, Hotwire, and Rails.
In your controller, you have a typical Rails action.
def show
@user = User.find(params[:id])
end
In your view, place a standard rails button_to
inside a turbo_frame_tag
and set the method
:
<%= turbo_frame_tag "friend" do %>
<%= button_to user_path(@user), method: :put do %>
<span class="font-bold <%= @user.is_friend? ? 'text-green-500' : 'text-gray-900' %>">Friend</span>
<% end %>
<% end %>
And in your controller, toggle your friend code and simply rerender that same view:
def update
# Write some code here to toggle the friend status
# ...
# Rerender the same view
render 'button'
end