ruby-on-railsrubyinternationalizationactionmailerrails-i18n

Why won't Rails' with_locale method allow additional URL parameters?


I've got this Mailer in my Rails 7 project:

# app/mailers/quote_mailer.rb

class QuoteMailer < ApplicationMailer

  def quote_mail(quote)
    @quote = quote
    @user = quote.user
    @client = quote.client
    @account = quote.account
    I18n.with_locale(@quote.locale) do
      mail(
        :from     => @user.email,
        :to       => @client.email,
        :subject  => "New Quote"
      )
    end
  end

end

# app/views/mailer/quote_mail.text.erb

Dear <= @client.name %>

Please click here to download your Quote:

<%= quote_download_url(@quote.token, :namespace => @account.namespace) %>

Best regards

<%= @user.name %>

# config/routes.rb

Rails.application.routes.draw do

  scope ':locale', :locale => /#{I18n.available_locales.join("|")}/ do

    scope ':namespace' do

      resources :quote_downloads, :only => [:show]

    end

  end

end

Unfortunately, the link in the email can't be built and throws an error:

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"quote_downloads", :locale=>"VmKHoRSx79AEatcoHtPz8uun", :namespace=>"volkswagen"}, missing required keys: [:id], possible unmatched constraints: [:locale]

It seems that my namespace parameter confuses Rails.

How can this be fixed?


Solution

  • I would not expect I18n.with_locale to set locale in URLs unless locale is configured as default URL option. I would only expect it to change the locale of the rendered email.

    But you can just pass it manually in your mailer like this:

    app/views/mailer/quote_mail.text.erb

    Dear <= @client.name %>
    
    Please click here to download your Quote:
    
    <%= quote_download_url(@quote.token, namespace: @account.namespace, locale: @quote.locale) %>
    
    Best regards
    
    <%= @user.name %>