努力理解这种语法错误 - Rails教程第8章

时间:2015-11-03 12:14:22

标签: ruby-on-rails ruby-on-rails-3 session railstutorial.org

对于Ruby on Rails,我是一个完全新手,我无法弄清楚导致代码中出现此语法错误的原因!

作为参考,我跟随Michael Hartl的Rails教程,目前正在进行第8章" 8.15 A Flash Test"。

在创建并填写闪存错误的集成测试后,我在运行rake测试时收到此错误:

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>'

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

路线档案:

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

请帮忙。我无法理解为什么会出现这种语法错误。

1 个答案:

答案 0 :(得分: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