diff --git a/app/controllers/cities_controller.rb b/app/controllers/cities_controller.rb index 140d02c..571c59e 100644 --- a/app/controllers/cities_controller.rb +++ b/app/controllers/cities_controller.rb @@ -1,4 +1,5 @@ class CitiesController < ApplicationController + include TagHelper before_action :authenticate_user!, only: [ :generate_weather_art ] before_action :require_admin, only: [ :generate_weather_art ] diff --git a/app/controllers/rss_controller.rb b/app/controllers/rss_controller.rb index 0aaaf68..6c00d95 100644 --- a/app/controllers/rss_controller.rb +++ b/app/controllers/rss_controller.rb @@ -1,4 +1,5 @@ class RssController < ApplicationController + include TagHelper def feed @weather_arts = WeatherArt.order(created_at: :desc).includes(:image_attachment, city: [ :country, :state ]).limit(20) @@ -27,11 +28,11 @@ class RssController < ApplicationController date_published: art.created_at.iso8601, image: art.image.attached? ? rails_blob_url(art.webp_image.processed) : nil, metadata: { - country: art.city&.country&.name, - city: art.city&.name, - state: art.city&.state&.name, - region: art.city&.region&.name, - subregion: art.city&.subregion&.name, + country: format_as_tag(art.city&.country&.name), + city: format_as_tag(art.city&.name), + state: format_as_tag(art.city&.state&.name), + region: format_as_tag(art.city&.region&.name), + subregion: format_as_tag(art.city&.subregion&.name), description: art.description, prompt: art.prompt, date: art.weather_date&.strftime("%Y-%m-%d"), diff --git a/app/controllers/weather_arts_controller.rb b/app/controllers/weather_arts_controller.rb index 8ea6451..ed8ab63 100644 --- a/app/controllers/weather_arts_controller.rb +++ b/app/controllers/weather_arts_controller.rb @@ -1,4 +1,5 @@ class WeatherArtsController < ApplicationController + include TagHelper def show @city = City.friendly.find(params[:city_id]) @weather_art = @city.weather_arts.friendly.find(params[:slug]) diff --git a/app/helpers/tag_helper.rb b/app/helpers/tag_helper.rb new file mode 100644 index 0000000..d04c46d --- /dev/null +++ b/app/helpers/tag_helper.rb @@ -0,0 +1,14 @@ +module TagHelper + def format_as_tag(text) + return nil if text.blank? + + # 下划线方式 (选项1) + text.strip.gsub(/\s+/, "_") + + # 或者删除空格 (选项2) + # text.strip.gsub(/\s+/, '') + + # 或者驼峰式 (选项3) + # text.strip.gsub(/\s+(\w)/) { $1.upcase }.gsub(/\s+/, '') + end +end diff --git a/app/views/cities/show.html.erb b/app/views/cities/show.html.erb index 9a9d992..2965577 100644 --- a/app/views/cities/show.html.erb +++ b/app/views/cities/show.html.erb @@ -87,7 +87,7 @@ <%= render "shared/share_social", title: share_title, description: share_description, - tags: "AIWeather,Art,AIart,Weather,#{@city&.name},#{@city&.country&.name}", + tags: "AIWeather,Art,AIart,Weather,#{format_as_tag(@city&.name)},#{format_as_tag(@city&.country&.name)},#{format_as_tag(@city&.country&.region&.name)},#{format_as_tag(@city&.country&.subregion&.name)}", image: url_for(@city&.latest_weather_art&.webp_image&.processed) %> diff --git a/app/views/weather_arts/show.html.erb b/app/views/weather_arts/show.html.erb index f217800..119affc 100644 --- a/app/views/weather_arts/show.html.erb +++ b/app/views/weather_arts/show.html.erb @@ -119,7 +119,7 @@ <%= render "shared/share_social", title: share_title, description: share_description, - tags: "AIWeather,Art,AIart,Weather,#{@weather_art.city&.name},#{@weather_art&.city&.country&.name}", + tags: "AIWeather,Art,AIart,Weather,#{format_as_tag(@weather_art.city&.name)},#{format_as_tag(@weather_art.city&.country&.name)},#{format_as_tag(@weather_art.city&.country&.region&.name)},#{format_as_tag(@weather_art.city&.country&.subregion&.name)}", image: url_for(@weather_art.webp_image.processed) %>