ruby-on-rails - 具有语言环境的高压 gem 路由

标签 ruby-on-rails ruby routes high-voltage

我正在使用 highvoltage gem 进行静态页面服务。它的工作,但网址是丑陋的。目前 url 如下所示:

localhost:3000/pages/terms-and-conditions?locale=en

但我希望 url 如下所示:

localhost:3000/en/pages/terms-and-conditions

在我写的路由文件中

scope ":locale", locale: /en|bn|hi/ do
  match "pages/:id" => 'pages#show', :as => :page, :format => false
end

然后我在 View 中写道:

<%=link_to "Terms & Conditions", page_path(:id=>'terms-and-conditions')%>

在我写的页面 Controller 中

  def show
    render params[:id]
  end

我现在该怎么做才能解决这个问题

最佳答案

我在 GitHub 上更新了这个问题: https://github.com/thoughtbot/high_voltage/issues/144

这是我在 GitHub 问题上发布的说明:

向您的应用程序 Controller 添加前置过滤器

# app/controllers/application_controller.rb
before_action :set_locale

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

禁用默认的高压路由

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.routes = false
end

在路由文件中设置嵌套的 URL

# config/routes.rb
scope "/:locale", locale: /en|bn|hi/ do
  get "/pages/:id" => 'high_voltage/pages#show', :as => :page, :format => false
end

向站点添加页面

# app/views/pages/about.html.erb
<%= t 'hello' %>

确保有对应的locale文件

/config/locale/en.yml
/config/locale/bn.yml

最后要注意的是有一个 know issue带高压。

您需要像这样指定路由 <%= link_to 'About Us', page_path(id: 'about') %>

如果您对此仍有疑问,请告诉我,我可以添加更多详细信息。

关于ruby-on-rails - 具有语言环境的高压 gem 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19809898/

相关文章:

AngularJS - 触发 Controller 时获取先前的路线

ruby-on-rails - Rails 生产服务器 : stylesheets not displaying!

ruby-on-rails - 在rails中使用路线时,_url和_path有什么区别

sql - 根据 ActiveRecord 关联和值获取对象集合

ios - 从 iPhone 通过的 Stripe 卡身份验证在服务器上失败

ruby-on-rails - sleep 在 sidekiq worker 中是否正常工作?

Ruby Datamapper 存储以科学记数法显示的小数

ruby-on-rails - 用户在新字符串上没有方法错误

ruby-on-rails - bool 值为假且属性存在的文章 - Rails 4

ruby-on-rails - 打开Spree::User类并添加功能而不丢失现有功能