I18n路由错误

时间:2016-12-16 21:14:59

标签: ruby-on-rails ruby internationalization translation

我正在通过I18n在我的应用中实现内部化。

我已更改路由:

    root 'sessions#new'
    get '/signup', to: 'users#new'
    post '/signup', to: 'users#create'
    get '/login', to: 'sessions#new'
    post '/login', to: 'sessions#create'
    delete '/logout', to: 'sessions#destroy'
    resources :users, only: [:new, :show, :create, :index] 

要:

  scope "(:locale)", locale: /pl|en/ do
    root 'sessions#new'
    get '/signup', to: 'users#new'
    post '/signup', to: 'users#create'
    get '/login', to: 'sessions#new'
    post '/login', to: 'sessions#create'
    delete '/logout', to: 'sessions#destroy'
    resources :users, only: [:new, :show, :create, :index] 
  end  

要从www.example.com/users?locale=pl更改为www.example.com/pl/users

测试时

  test "should redirect show user when not logged in" do
    get user_path(@user)
    assert_not flash.empty?
    assert_redirected_to root_url
  end

我遇到了一个问题。

  

错误:

UsersControllerTest#test_should_redirect_show_user_when_not_logged_in:
> 
>     ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"users", :locale=>#<User id: 753762654,
> first_name: "Jan", last_name: "Kowalski", password: "test12",
> student_id: "123456", created_at: "2016-12-16 21:02:17", updated_at:
> "2016-12-16 21:02:17", cookies_digest: nil, admin: true>} missing
> required keys: [:id]
>     test/controllers/users_controller_test.rb:16:in `block<class:UsersControllerTest>'

如何在保持网址的同时解决这个问题:example.com/pl/users?

1 个答案:

答案 0 :(得分:1)

在测试

中调用printf("%2d",num)时,您应该传递所需的locale

喜欢这个user_path