Setting default_url_options for mounted Rails engine

爱⌒轻易说出口 提交于 2019-11-30 15:30:29
pmuens

Faced exactly the same problem and I have found a solution for that...

Here is my application_controller-File (my engines inherit from this file (which is the Main Apps ApplicationController, so that I don't have code-duplication)

#!/bin/env ruby
# encoding: utf-8
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :set_locale_from_params

  def url_options
    { locale: I18n.locale }
  end

  protected
    def set_locale_from_params
      if params[:locale]
        if I18n.available_locales.include?(params[:locale].to_sym)
          I18n.locale = params[:locale]
        else
          flash.now[:notice] = 'Translation not available'
          logger.error flash.now[:notice]
        end
      end
    end
end

Note, that the url_options-code is outside the protected part. It has to be public.

Found the tipps for the solution here: default_url_options and rails 3

Hope it helps

Regards

Philipp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!