没有传递语言环境时,Mailboxer gem的邮件程序会导致ActionController :: UrlGenerationError

时间:2015-12-11 16:16:46

标签: ruby-on-rails locale mailboxer

我正在尝试使用Mailboxer gem在我的Rails 4应用程序中获取用户到用户的消息传递系统。但是,我在发送邮件时在浏览器中收到以下错误:

ActionController::UrlGenerationError in Conversations#create

罪魁祸首是以下邮件模板(new_message_email.html.erb):

Visit <%= link_to "this page", root_url %> and go to your inbox for more info.

原因是

No route matches {:action=>"index", :controller=>"rooms"} missing required keys: [:locale]

这是因为我的整个应用程序的路由需要一个区域设置。我的routes.rb看起来像这样:

Rails.application.routes.draw do
  get '' => redirect("/#{I18n.default_locale}")
  scope "/:locale", locale: /en|ja|ko/ do
    root 'rooms#index'
    resources :rooms
    # ... All my other routes here ...
  end
end

有趣的是,控制台出现以下错误(与浏览器中的错误不同):

ActionView::Template::Error (No route matches {:action=>"index", :controller=>"rooms"} missing required keys: [:locale]):
    14:       </p>
    15:     </blockquote>
    16:     <p>
    17:       Visit <%= link_to "this page", root_url %> and go to your inbox for more info.
    18:     </p>
    19:   </body>
    20: </html>

但我认为模板不是问题 - 真正的问题是,之前发生了一些需要传递区域设置的呼叫,因此root_url失败,因为它没有“知道要使用的语言环境。

我已经在我的application_controller.rb中传递了以下内容:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :set_locale

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

  def default_url_options(options = {})
    { locale: I18n.locale }.merge options
  end
end

是否有人知道如何解决此问题或如何将区域设置传递给邮件程序root_url

另外,如果需要更多代码,请告诉我。

提前致谢:)

1 个答案:

答案 0 :(得分:0)

巴恩,傻错!指定语言环境似乎使其有效:

<%= link_to "this page", root_url(locale: :en) %>

虽然理想情况下此设置应取决于用户的首选语言。