使用ActionMailer路由问题"缺少必需的密钥:[:locale]"

时间:2014-12-16 14:21:09

标签: routes actionmailer rails-i18n

我最近为我的rails应用添加了多语言支持,但现在我的所有邮件都停止了工作......

class NotificationUserMailer < ActionMailer::Base
  default from: 'control-de-asistencia@email.us'

  def send_welcome_mail_to(user_id, password)
    @user     = User.find(user_id)
    @password = password
    mail(to: @user.email, subject: 'Bienvenido a Timewarp')
  end
end

每次到达

mail(to: @user.email, subject: 'Bienvenido a Timewarp')

我得到以下例外:

  

::的ActionView模板::错误:          没有路由匹配{:action =&gt;“new”,:controller =&gt;“devise / sessions”}缺少必需的密钥:[:locale]

这是我的代码

class ApplicationController < ActionController::Base
  before_action :set_locale
  private

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

  private

  def set_locale
    I18n.locale = user_signed_in? ? current_user.language.to_sym : I18n.default_locale
  end
end

环境

#development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000, protocol: 'http', locale: I18n.locale }
  config.action_mailer.asset_host = 'http://localhost:3000'
  config.assets.raise_runtime_errors = false
  config.action_mailer.preview_path = "#{Rails.root}/spec/mailers/previews"
  # config.action_view.raise_on_missing_translations = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default charset: 'utf-8'
  config.action_mailer.smtp_settings = {
    address: 'smtp.mandrillapp.com',
    port: 587,
    domain: Rails.application.secrets.domain_name,
    enable_starttls_auto: true,
    user_name: Rails.application.secrets.mandrill_username,
    password:  Rails.application.secrets.mandrill_api_key
  }

路线:

Rails.application.routes.draw do
  scope ':locale', locale: /#{I18n.available_locales.join('|')}/ do
    devise_for :users

    devise_scope :user do
      authenticated :user do
        root 'dashboard#show', as: :authenticated_root
      end

      unauthenticated do
        root 'devise/sessions#new', as: :unauthenticated_root
      end
    end

    root to: 'devise/sessions#new'
  end
    get '*path', to: redirect("/#{I18n.default_locale}/%{path}")
    get '', to: redirect("/#{I18n.default_locale}")
end

规格:

describe '#send_welcome_mail_to' do
    it 'should send mail to user' do
      user = create(:user)
      expect { NotificationUserMailer.send_welcome_mail_to(user.id, user.password).deliver }.to change { ActionMailer::Base.deliveries.count }.by(1)
    end
  end

我是否必须在其他地方设置区域设置ApplicationController.rb ??我现在真的迷路了 任何帮助都会很棒..谢谢!

1 个答案:

答案 0 :(得分:0)

我有一个&#34; root_path#34;在我的视图中,不会正确设置区域设置。我创建了视图之外的路径......

class NotificationUserMailer < ActionMailer::Base
  default from: 'control-de-asistencia@email.us'

  def send_welcome_mail_to(user_id, password)
    @user     = User.find(user_id)
    @password = password
   @root_url = url_for(controller: 'devise/sessions', action: 'new', locale: @user.language)
    mail(to: @user.email, subject: 'Bienvenido a Timewarp')
  end
end