Rails 3 Rspec 2 问题:locale 和 RESTfull 路径

发布于 2024-10-16 04:13:45 字数 2598 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空心空情空意 2024-10-23 04:13:45

好的,我在测试中找到了解决此问题的方法(参见 https://github.com /rspec/rspec-rails/issues/255

我把这段代码放在spec_helper.rb中

class ActionView::TestCase 
  class TestController
    def default_url_options
      {:locale => 'en'}
    end
  end
end

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

class ActionView::TestCase 
  class TestController
    def default_url_options
      {:locale => 'en'}
    end
  end
end
疯狂的代价 2024-10-23 04:13:45

当您使用语言环境时,也许 url_for 无法推断出正确的参数?试试这样:

visit edit_user_path(:id=>user.id)

Maybe url_for is unable to infer the right parameters when you use a locale? Try it this way:

visit edit_user_path(:id=>user.id)
策马西风 2024-10-23 04:13:45

将其包含在 spec_helper.rb 中对我有用:

config.before do
  default_url_options[:locale] = I18n.default_locale
end

Including this in spec_helper.rb worked for me:

config.before do
  default_url_options[:locale] = I18n.default_locale
end
夏雨凉 2024-10-23 04:13:45

我尝试将其放入 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 }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文