ruby 为什么Rails的with_locale方法不允许附加URL参数?

fcipmucu  于 2023-08-04  发布在  Ruby
关注(0)|答案(1)|浏览(107)

我在Rails 7项目中有这个Mailer

# 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

不幸的是,电子邮件中的链接无法构建并抛出错误:
ActionView::Template::Error(No route matches {:action=>“show”,:controller=>“quote_downloads”,:locale=>“VmKHoRSx79AEatcoHtPz8uun”,:namespace=>“volkswagen”},missing required keys:[:id],可能不匹配的约束:[:locale]
似乎我的namespace参数混淆了Rails。
如何解决这个问题?

w51jfk4q

w51jfk4q1#

我不希望I18n.with_locale在URL中设置locale,除非locale被配置为默认URL选项。我只希望它能改变呈现的电子邮件的区域设置。
但是你可以像这样在你的邮件中手动传递它:

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 %>

字符串

相关问题