From a87815711a8fd9fc47a1d90fbdd14236432c9cdf Mon Sep 17 00:00:00 2001 From: stawan15 Date: Thu, 16 Jul 2026 15:24:54 +0700 Subject: [PATCH 1/2] fix: simplify URL result handling by removing redundant updates and auto-refresh --- app/views/urls/index.html.erb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/views/urls/index.html.erb b/app/views/urls/index.html.erb index 953c028..8926eed 100644 --- a/app/views/urls/index.html.erb +++ b/app/views/urls/index.html.erb @@ -348,12 +348,7 @@ }); const data = await resp.json(); if (data.error) throw new Error(data.error); - - resultUrl.href = data.long_url; - resultUrl.textContent = data.long_url; - copyBtn.dataset.url = data.long_url; - result.hidden = false; - setTimeout(() => window.location.reload(), 2000); + window.location.reload(); } catch (e) { errEl.textContent = e.message; errEl.hidden = false; From 5f463bd6bb424aaf7b0329e111606221ca1dd677 Mon Sep 17 00:00:00 2001 From: stawan15 Date: Thu, 16 Jul 2026 15:26:22 +0700 Subject: [PATCH 2/2] feat: implement ActionCable channels for real-time URL updates and create partial for URL rendering --- app/channels/application_cable/channel.rb | 4 +++ app/channels/application_cable/connection.rb | 4 +++ app/controllers/urls_controller.rb | 15 +++++++-- app/views/urls/_url.html.erb | 27 +++++++++++++++ app/views/urls/index.html.erb | 35 +++----------------- 5 files changed, 53 insertions(+), 32 deletions(-) create mode 100644 app/channels/application_cable/channel.rb create mode 100644 app/channels/application_cable/connection.rb create mode 100644 app/views/urls/_url.html.erb diff --git a/app/channels/application_cable/channel.rb b/app/channels/application_cable/channel.rb new file mode 100644 index 0000000..d672697 --- /dev/null +++ b/app/channels/application_cable/channel.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Channel < ActionCable::Channel::Base + end +end diff --git a/app/channels/application_cable/connection.rb b/app/channels/application_cable/connection.rb new file mode 100644 index 0000000..0ff5442 --- /dev/null +++ b/app/channels/application_cable/connection.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Connection < ActionCable::Connection::Base + end +end diff --git a/app/controllers/urls_controller.rb b/app/controllers/urls_controller.rb index c5ced1d..9b302d9 100644 --- a/app/controllers/urls_controller.rb +++ b/app/controllers/urls_controller.rb @@ -22,6 +22,7 @@ def create @url.user = current_user if user_signed_in? if @url.save + broadcast_url_to_user(@url) if user_signed_in? redirect_to root_path, notice: "Short URL created successfully!" else @@ -60,8 +61,8 @@ def lengthen @url.short_code = generate_long_code if @url.save - long_url = short_url_for(@url) - render json: { long_url: long_url } + broadcast_url_to_user(@url) + render json: { long_url: short_url_for(@url) } else render json: { error: @url.errors.full_messages.first }, status: :unprocessable_entity end @@ -163,6 +164,16 @@ def throttle_expand! render json: { error: "Too many requests. Please slow down." }, status: :too_many_requests end + def broadcast_url_to_user(url) + return unless url.user_id.present? + Turbo::StreamsChannel.broadcast_prepend_to( + "urls_#{url.user_id}", + target: "urls_list", + partial: "urls/url", + locals: { url: url } + ) + end + def permitted_url_params allowed = [:original_url] allowed << :short_code if user_signed_in? diff --git a/app/views/urls/_url.html.erb b/app/views/urls/_url.html.erb new file mode 100644 index 0000000..26b2f74 --- /dev/null +++ b/app/views/urls/_url.html.erb @@ -0,0 +1,27 @@ +<% kind = url.short_code.length > 20 ? "long" : "short" %> + diff --git a/app/views/urls/index.html.erb b/app/views/urls/index.html.erb index 8926eed..f7ae7a5 100644 --- a/app/views/urls/index.html.erb +++ b/app/views/urls/index.html.erb @@ -103,42 +103,17 @@ -