feat: improve seo urls?
This commit is contained in:
@@ -27,10 +27,33 @@ class EventsController < ApplicationController
|
||||
private
|
||||
|
||||
# Find and set the current event with eager-loaded associations
|
||||
#
|
||||
# Supports both old slug-only format and new SEO-friendly year/month/slug format
|
||||
# Loads event with ticket types to avoid N+1 queries
|
||||
# Raises ActiveRecord::RecordNotFound if event doesn't exist
|
||||
def set_event
|
||||
@event = Event.includes(:ticket_types).find(params[:id])
|
||||
if params[:year] && params[:month]
|
||||
# New SEO-friendly format: /events/2024/07/summer-party
|
||||
year = params[:year].to_i
|
||||
month = params[:month].to_i
|
||||
start_of_month = Date.new(year, month, 1).beginning_of_month
|
||||
end_of_month = start_of_month.end_of_month
|
||||
|
||||
@event = Event.includes(:ticket_types)
|
||||
.where(slug: params[:slug])
|
||||
.where(start_time: start_of_month..end_of_month)
|
||||
.first!
|
||||
else
|
||||
# Legacy format: /events/summer-party (for backward compatibility)
|
||||
@event = Event.includes(:ticket_types).find_by!(slug: params[:slug])
|
||||
end
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
redirect_to events_path, alert: "Événement non trouvé"
|
||||
end
|
||||
|
||||
# Generate SEO-friendly path for an event
|
||||
def seo_event_path(event)
|
||||
year = event.start_time.year
|
||||
month = format("%02d", event.start_time.month)
|
||||
event_path(year: year, month: month, slug: event.slug)
|
||||
end
|
||||
helper_method :seo_event_path
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user