before_filter set_locale除了控制器
我的routes.rb
MyApp::Application.routes.draw do scope '(:locale)' do #all resources here end namespace :blog do resources :posts, :only => [:index, :show] end end
我的application_controller.rb
class ApplicationController < ActionController::Base # # before_filter :set_locale private def default_url_options(options = {}) {locale: I18n.locale} end def set_locale #code for detect locale here end # # end
scope '(:locale)'
内的所有资源都正常工作。
但是我不想使用带有namespace :blog
语言环境namespace :blog
,当我尝试点击博客链接时,我可以看到这个urlhttp://localhost:3000/blog/posts?locale=en
如何删除namespace :blog...
区域设置namespace :blog...
和blog resource
? 我想得到一个类似http://localhost:3000/blog/posts
的url我想删除?locale=en
谢谢!
在Blog控制器中使用skip_before_filter
?
鉴于您在评论中所说的内容,如果当前控制器不是PostsController
,请尝试仅在default_url_options
包含locale
,这有望摆脱尾随
?locale=en
issue。 也许是这样的:
def default_url_options(options = {}) { locale: I18n.locale } unless controller_name == 'posts' end
或者,由于default_url_options
被折旧 ,如果您想使用url_options
,可能类似于:
def url_options controller_name == 'posts' ? super : { locale: I18n.locale }.merge(super) end
以上都没有测试过,所以我不确定它们中的任何一个都能正常工作。
编辑
如果您将此locale
为nil
就像在此StackOverflow问答中一样? 也许是这样的:
def url_options locale = controller_name == 'posts' ? nil : I18n.locale { locale: locale }.merge(super) end