路线:无法使用默认范围(:locale)与user_path(id)配合使用,始终必须显式提交语言环境 [英] Routes: can't get default scope(:locale) to work with user_path(id), always have to submit locale explicitly
问题描述
我已经对此主题进行了一些研究,但似乎无法弄清楚.
I've done some research on this topic already, but I don't seem to be able to figure it out.
我已经按照官方指南设置了I18n,但我只是不明白正确设置的默认语言环境(当未指定显式语言环境时).
I have followed the official guide to set up I18n, but I just don't get the default locale to be set properly (when no explicit locale is specified).
# routes.rb
require 'sidekiq/web'
Iq::Application.routes.draw do
scope "(:locale)", locale: /de|en/ do
# ...
end
end
# application_controller.rb
class ApplicationController < ActionController::Base
before_filter :set_language
def set_language
I18n.locale = params[:locale] || I18n.default_locale
end
def default_url_options(options = {})
options.merge!({ :locale => I18n.locale })
end
end
在OS X控制台中:
$ rake routes | grep user
...
user GET (/:locale)/users/:id(.:format) users#show {:locale=>/de|en/}
...
在Rails控制台中:
In the Rails console:
$ Rails c
$ app.users_path
=> "/users"
app.users_path locale: :de
=> "/de/users"
$ app.user_path User.first, locale: :de
=> "/de/users/509fc01d77bb1e6a050000a0"
$ app.user_path User.first
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"users", :locale=>#<User _id: 509fc01d77bb1e6a050000a0, _type: nil, created_at: 2012-11-11 15:11:25 UTC, updated_at: 2012-11-11 15:11:25 UTC, deleted_at: nil, group: "administrator", language: "de", active: true, sign_in_count: 0, name: "sysadmin", email: "support@sientia.ch", encrypted_password: "$2a$10$n/b7sTmUjEMoZI/jvq2jPuaNQqo1R1zbAIPpko9HT9PERagXclrPK", reset_password_token: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: nil, unconfirmed_email: nil, reset_password_sent_at: nil, remember_created_at: nil, confirmed_at: 2012-11-11 15:11:25 UTC, confirmation_sent_at: nil, current_sign_in_at: nil, last_sign_in_at: nil, save_vertical_menu_visibility_state: nil, contact_id: "509fc01d77bb1e6a0500008c", contact_name: "Sientia AG">}
from /Users/josh/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:532:in `raise_routing_error'
为什么这行不通?我忘记了什么?
Why doesn't this work? What did I forget?
非常感谢您的帮助, 乔什
Thanks a lot for help, Josh
推荐答案
同事A对其进行了调查,并决定我不应该将(:locale)
放在方括号中.另一个同事B提到,他将其放在方括号中,以便我们的测试有效,在这里我们不想在调用url helper方法时显式指定语言环境.
A co-worker A looked into it, and decided that I shouldn't put (:locale)
into brackets. Another co-worker B mentioned, that he put it into brackets so that our tests work, where we do not want to specify the locale explicitly when calling an url helper method.
同事A提到,当在没有语言环境的情况下调用url helper方法时,应用程序本身将退回到上述的default_url_options
方法.很好,因为除了root_path
之外,我们始终想要一个显式的语言集.
Co-worker A mentioned, that the application itself falls back to the default_url_options
method mentioned above, when an url helper method is called without locale. This is great, because except the root_path
, we always want an explicit language set.
尽管在测试中,default_url_options
方法会被忽略(我不确定这是错误还是功能,哈哈).因此,必须采取一些解决方法:
In tests though, the default_url_options
method is ignored (I'm not sure whether this is a bug or a feature, haha). So one has to do some workarounds:
# Fixes the missing default locale problem in request specs.
# See http://www.ruby-forum.com/topic/3448797
class ActionView::TestCase::TestController
def default_url_options(options={})
{ :locale => I18n.default_locale }
end
end
class ActionDispatch::Routing::RouteSet
def default_url_options(options={})
{ :locale => I18n.default_locale }
end
end
# Fixes the missing default locale problem in controller specs
# See http://www.ruby-forum.com/topic/3448797#1041659
class ActionController::TestCase
module Behavior
def process_with_default_locale(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
parameters = { :locale => I18n.default_locale }.merge( parameters || {} )
process_without_default_locale(action, parameters, session, flash, http_method)
end
alias_method_chain :process, :default_locale
end
end
module ActionDispatch::Assertions::RoutingAssertions
def assert_recognizes_with_default_locale(expected_options, path, extras = {}, message=nil)
expected_options = { :locale => I18n.default_locale.to_s }.merge(expected_options || {} )
assert_recognizes_without_default_locale(expected_options, path, extras, message)
end
alias_method_chain :assert_recognizes, :default_locale
end
将其放入您的spec_helper.rb文件中,即可开始使用控制器并索取规格! :-)
Put this into your spec_helper.rb file, and you're ready to go with your controller and request specs! :-)
仍然,在控制台中,这似乎并没有解决问题:
Still, in the console, this doesn't seem to do the trick:
$ app.user_path User.first
ActionController::RoutingError: No route matches...
这篇关于路线:无法使用默认范围(:locale)与user_path(id)配合使用,始终必须显式提交语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!