Problems with :locale in ApplicationController and SubscriptionsMailer
Opened this issue · 0 comments
dannatobigodo commented
In a project we use a scope in routes for language switching, which includes the commontator mount:
scope path: '(:locale)', constraints: { locale: /#{I18n.available_locales.join("|")}/ } do
mount Commontator::Engine => '/commontator'
# ...
end
We had to customize ApplicationController and SubscriptionMailer due to routing errors in using url helpers:
class Commontator::ApplicationController < ActionController::Base
before_action :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def url_options(options = {})
options.merge(locale: I18n.locale, only_path: true)
end
# ...
end
class Commontator::SubscriptionsMailer < ActionMailer::Base
def url_options
{ locale: I18n.locale }.merge(super)
end
# ...
end
In particular, Commontator failed to send notifications of new comments, because the :locale parameter was filled with the commontable object, resulting in the error: "No route matches ... missing required keys: [: id]".
Also, after creating a comment, the current :locale was not passed and translations in the default language appeared.