retrospring/app/controllers/subscriptions_controller.rb
Andreas Nedbal 4408de33cc Setup turbo_stream_actions before before_actions
Because apparently the error catching -> toast logic was not there yet and people might run into a 500 error redirect when pressing the button.
2025-02-21 23:25:22 +01:00

42 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class SubscriptionsController < ApplicationController
include TurboStreamable
turbo_stream_actions :create, :destroy
before_action :authenticate_user!
before_action :not_readonly!
def create
answer = Answer.find(params[:answer])
result = Subscription.subscribe(current_user, answer)
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("subscription-#{answer.id}", partial: "subscriptions/destroy", locals: { answer: }),
render_toast(t(result.present? ? ".success" : ".error"), result.present?)
]
end
format.html { redirect_to answer_path(username: answer.user.screen_name, id: answer.id) }
end
end
def destroy
answer = Answer.find(params[:answer])
result = Subscription.unsubscribe(current_user, answer)
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("subscription-#{answer.id}", partial: "subscriptions/create", locals: { answer: }),
render_toast(t(result.present? ? ".success" : ".error"), result.present?)
]
end
format.html { redirect_to answer_path(username: answer.user.screen_name, id: answer.id) }
end
end
end