2015-11-03 88 views
-1

当谈到Ruby on Rails时,我是一个完整的新手,我无法弄清楚在我的代码中导致这种语法错误的原因!努力理解这种语法错误 - Rails教程第8章

作为参考,我正在关注Michael Hartl的Rails教程,我目前正在编写第8章“8.15 Flash测试”。

ERROR["test_should_get_new", SessionsControllerTest, 2015-11-01 06:24:20 +0000] 
test_should_get_new#SessionsControllerTest (1446359060.17s) 
SyntaxError:   SyntaxError: /Users/user/charity_app/app/controllers/sessions_controller.rb:15: syntax error, unexpected end-of-input, expecting keyword_end 


ERROR["test_login_with_invalid_information", UsersLoginTest, 2015-11-01 06:24:20 +0000] 
test_login_with_invalid_information#UsersLoginTest (1446359060.67s) 
SyntaxError:   SyntaxError: /Users/user/charity_app/app/controllers/sessions_controller.rb:15: syntax error, unexpected end-of-input, expecting keyword_end 
     test/integration/users_login_test.rb:6:in `block in <class:UsersLoginTest>' 
    test/integration/users_login_test.rb:6:in `block in <class:UsersLoginTest>' 

这里是我的测试文件,路线文件和我的会话控制器文件:

中创建和集成测试的闪存错误填充后,我当我运行rake测试收到此错误

routes文件:

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 

会话控制器文件:

class SessionsController < ApplicationController 
def new 
end 

def create 
user = User.find_by(email: params[:session][:email].downcase) 
if user && user.authenticate(params[:session][:password]) 
    # log user in 
else 
    # show error message 
end 

def destroy 
end 
end 

users_login_test文件:

require 'test_helper' 

class UsersLoginTest < ActionDispatch::IntegrationTest 

test "login with invalid information" do 
get login_path 
assert_template 'sessions/new' 
post login_path, session: { email: "", password: "" } 
assert_template 'sessions/new' 
assert_not flash.empty? 
get root_path 
assert flash.empty? 
end 
end 

和我sessions_controller_test文件:

require 'test_helper' 

class SessionsControllerTest < ActionController::TestCase 
test "should get new" do 
get :new 
assert_response :success 
end 

end 

测试帮助文件:

ENV['RAILS_ENV'] ||= 'test' 
require File.expand_path('../../config/environment', __FILE__) 
require 'rails/test_help' 
require "minitest/reporters" 
Minitest::Reporters.use! 

class ActiveSupport::TestCase 
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical 
# order. 
fixtures :all 


# Add more helper methods to be used by all tests here... 
end 

请帮助。我不明白为什么这个语法错误正在发生。

+3

如果您正确缩进您的代码,您不会遇到发现此错误的问题。在会话控制器中'create'方法的末尾缺少'end'。 –

+0

它在我的文件上正确缩进,我只是不确定如何缩进这里。非常感谢! – feezy26

回答

0

提供正确的缩进,所以你不会错过如花括号,结束语句等行结束。

class SessionsController < ApplicationController 

def new 
end 

def create 
    user = User.find_by(email: params[:session][:email].downcase) 
    if user && user.authenticate(params[:session][:password]) 
     # log user in 
    else 
     # show error message 
    end 
end 

def destroy 
end 
end 
相关问题