Rails 3 Rspec 2 问题:locale 和 RESTfull 路径
我的 rspec 文件中的 edit_user_path(user)
有问题:
it "should forward to the requested page after signin" do
user = Factory(:user)
visit edit_user_path(user)
fill_in :email, :with => user.email
fill_in :password, :with => user.password
click_button
response.should render_template('users/edit')
end
我收到此错误:
Failure/Error: visit edit_user_path(user)
ActionController::RoutingError:
No route matches {:action=>"edit", :controller=>"users", :locale=>#<User id: 1, name: "Max Mustermann", email: "[email protected]", encrypted_password: "1b0befc5caf12c2abf53d99sdfsd5ba15ebe28362eae21b2fb3...", salt: "331ca07444f5dcee76605bsdfasd95c833b7d5135b36a253723...", created_at: "2011-02-03 18:54:34", updated_at: "2011-02-03 18:54:34">}
我不知道为什么它使用 user
作为 :locale 以及为什么
user.id
中没有 :id
。我使用 :locale
设置语言,例如 localhost:3000/en/home 或 localhost:3000/de/home。
这是我的routs.rb
:
Hoapp::Application.routes.draw do
scope '(:locale)', :locale => /de|en/ do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :hotels, :only => [:create, :destroy]
root :to => 'pages#home'
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/about', :to => 'pages#about'
match '/blog', :to => 'pages#blog'
match '/contact', :to => 'pages#contact'
match '/help', :to => 'pages#help'
match '/overview', :to => 'pages#overview'
match '/preview', :to => 'pages#preview'
match '/settings', :to => 'pages#settings'
match 'm', :to => 'mobile#home'
match 'apartments', :to => 'mobile#apartments'
match 'apartment_info', :to => 'mobile#apartment_info'
match 'apartment_images', :to => 'mobile#apartment_images'
match 'apartment_price', :to => 'mobile#apartment_price'
match 'wellness', :to => 'mobile#wellness'
match 'test', :to => 'mobile#test'
scope "m" do
resources :requests, :only => [:new, :create]
match 'request_sent', :to => 'requests#sent'
end
match ':vanity_url/', :to => "mobile#home"
end
I have a problem with edit_user_path(user)
in my rspec file:
it "should forward to the requested page after signin" do
user = Factory(:user)
visit edit_user_path(user)
fill_in :email, :with => user.email
fill_in :password, :with => user.password
click_button
response.should render_template('users/edit')
end
I get this error:
Failure/Error: visit edit_user_path(user)
ActionController::RoutingError:
No route matches {:action=>"edit", :controller=>"users", :locale=>#<User id: 1, name: "Max Mustermann", email: "[email protected]", encrypted_password: "1b0befc5caf12c2abf53d99sdfsd5ba15ebe28362eae21b2fb3...", salt: "331ca07444f5dcee76605bsdfasd95c833b7d5135b36a253723...", created_at: "2011-02-03 18:54:34", updated_at: "2011-02-03 18:54:34">}
I have no clue why it uses the user
as :locale
and why there is no :id
with the user.id
. I use :locale
to set the language, e.g. localhost:3000/en/home or localhost:3000/de/home.
This is my routs.rb
:
Hoapp::Application.routes.draw do
scope '(:locale)', :locale => /de|en/ do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :hotels, :only => [:create, :destroy]
root :to => 'pages#home'
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/about', :to => 'pages#about'
match '/blog', :to => 'pages#blog'
match '/contact', :to => 'pages#contact'
match '/help', :to => 'pages#help'
match '/overview', :to => 'pages#overview'
match '/preview', :to => 'pages#preview'
match '/settings', :to => 'pages#settings'
match 'm', :to => 'mobile#home'
match 'apartments', :to => 'mobile#apartments'
match 'apartment_info', :to => 'mobile#apartment_info'
match 'apartment_images', :to => 'mobile#apartment_images'
match 'apartment_price', :to => 'mobile#apartment_price'
match 'wellness', :to => 'mobile#wellness'
match 'test', :to => 'mobile#test'
scope "m" do
resources :requests, :only => [:new, :create]
match 'request_sent', :to => 'requests#sent'
end
match ':vanity_url/', :to => "mobile#home"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,我在测试中找到了解决此问题的方法(参见 https://github.com /rspec/rspec-rails/issues/255)
我把这段代码放在spec_helper.rb中
Ok I found a way to fix this in the tests (cf. https://github.com/rspec/rspec-rails/issues/255)
I put this code in spec_helper.rb
当您使用语言环境时,也许
url_for
无法推断出正确的参数?试试这样:Maybe
url_for
is unable to infer the right parameters when you use a locale? Try it this way:将其包含在
spec_helper.rb
中对我有用:Including this in
spec_helper.rb
worked for me:我尝试将其放入 config/environments/test.rb 并为我工作:
config.action_controller.default_url_options = { locale: I18n.locale }
I tried to put this in
config/environments/test.rb
and worked for me:config.action_controller.default_url_options = { locale: I18n.locale }