我正在使用 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/