Rails:Signout链接的路由错误

时间:2014-08-09 22:15:50

标签: ruby-on-rails rspec

大家好,我正在研究Hartl的Rails教程第8.2.6章

我的应用程序未通过我为测试'注销'而编写的测试。链接。

这是我的命令提示符的副本

C:\Sites\sample_app>bundle exec rspec spec/
.............F.................................

Failures:

  1) Authentication signin with valid information followed by signout
     Failure/Error: before { click_link "Sign out" }
     ActionController::RoutingError:
       uninitialized constant SessionController
     # ./spec/requests/authenticate_pages_spec.rb:35:in `block (5 levels) in <to
p (required)>'

Finished in 1.17 seconds
47 examples, 1 failure

Failed examples:

rspec ./spec/requests/authenticate_pages_spec.rb:36 # Authentication signin with
 valid information followed by signout

以下是测试代码的相关部分(spec / requests / authenticate_pages_spec.rb)

描述&#34;然后注销&#34;做     在{click_link&#34;退出&#34; }     它{应该有_link(&#39;登录&#39;)}    端

应用程序/控制器/ sessions_controller.rb

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_to user
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_url
  end
end

配置/路由

SampleApp::Application.routes.draw do 
  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  root  'static_pages#home'
  match '/signup',  to: 'users#new',            via: 'get'
  match '/signin',  to: 'sessions#new',         via: 'get'
  match '/signout', to: 'session#destroy',      via: 'delete'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'

1 个答案:

答案 0 :(得分:0)

此处的指标为uninitialized constant SessionController

您的控制器名称是复数,SessionsController

如果您查看routes.rb文件,就会发现错误。

match '/signout', to: 'session#destroy',      via: 'delete'

应该是这样的:

match '/signout', to: 'sessions#destroy',      via: 'delete'