我正在关注Hartl" Rails Tutorial,"到目前为止,一切都进展顺利。但是,我坚持第8.3节。我已经对我的代码进行了三重检查,但测试注销功能仍然会抛出MissingTemplate错误。我可以在/ app / views中创建一个,但我认为这不是解决此错误的正确方法。
你可以给这位新秀提供任何帮助,我将不胜感激。谢谢!
users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:success] = "Welcome!"
redirect_to @user
else
render 'new'
end
end
def destroy
log_out
redirect_to root_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
的routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
end
sessions_helper.rb
module SessionsHelper
def log_in(user)
session[:user_id] = user.id
end
def log_out
session.delete(:user_id)
@current_user = nil
end
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
def logged_in?
!current_user.nil?
end
end
测试
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "login with valid information followed by logout" do
get login_path
post login_path, session: { email: @user.email, password: 'password' }
assert is_logged_in?
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
delete logout_path
assert_not is_logged_in?
assert_redirected_to root_url
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end
end
错误
ERROR["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 2015-10-07 09:40:37 +0900]
test_login_with_valid_information_followed_by_logout#UsersLoginTest (1444178437.65s)
ActionView::MissingTemplate: ActionView::MissingTemplate: Missing template sessions/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/home/computer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates"
* "/home/computer/Workspace/sample_app/app/views"
* "/home/computer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/app/views"
test/integration/users_login_test.rb:20:in `block in <class:UsersLoginTest>'
20/20: [=================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.60698s
20 tests, 45 assertions, 0 failures, 1 errors, 0 skips
答案 0 :(得分:1)
由于它正在尝试为destroy
中的sessions_controller.rb
操作自动呈现模板,因此您可能在该操作的方法中错过了redirect_to
。