动态范围内可安装Rails的引擎 [英] Rails mountable engine under a dynamic scope

查看:64
本文介绍了动态范围内可安装Rails的引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这些路线:

scope '(:locale)', :locale => /en|de/ do
  mount Users::Engine => "users", as: 'users_engine'  
end

并在引擎视图中:

<%= link_to 'new user', action: :new, controller: :users, locale: :de %>

我知道了

/en/users/users/new?locale=de 

而不是

/de/users/users/new

我已经包含在应用程序控制器中:

I have already included in application controller:

def set_locale
  if params.include?('locale')
    I18n.locale = params[:locale]
    Rails.application.routes.default_url_options[:locale] = I18n.locale
  end
end

,并且在主应用程序中效果很好

and it works fine in the main app

我找到了一种通过

Users::Engine.routes.url_for controller: 'users/users', action: :new, only_path: true, locale: :de

但我认为应该是一种更好的方法,如果我在布局中创建更改区域设置链接怎么办?

but I think that there should be a better way and what if I was making a change-locale link in the layout ?

<%= link_to locale: :de %>

我不知道这可能是哪个确切引擎

I cannot know which exact engine this could be

感谢虚拟机。

推荐答案

我想我已经找到了解决方案...

I think I have found a solution for that...

只需在application_controller.rb文件中使用url_options方法(请参见下面的链接)

Just use the url_options-method in your application_controller.rb-File (see link below)

请参阅:https://stackoverflow.com/a/18299975/603126

希望它会有所帮助!

致谢

Philipp

这篇关于动态范围内可安装Rails的引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆