【问题标题】:Struggling to understand this syntax error - Rails Tutorial Chapter 8努力理解这个语法错误 - Rails 教程第 8 章
【发布时间】:2015-11-03 12:14:21
【问题描述】:

当谈到 Ruby on Rails 时,我是一个完全的新手,我不知道是什么导致了我的代码中出现这种语法错误!

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

创建并填写flash错误的集成测试后,我在运行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

还有我的 session_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

请帮忙。我不明白为什么会出现这种语法错误。

【问题讨论】:

  • 如果你正确缩进你的代码,你不会有发现这个错误的问题。您在会话控制器中的 create 方法末尾缺少 end
  • 它在我的文件上被正确缩进了,我只是不确定如何在这里缩进。非常感谢!

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


【解决方案1】:

提供适当的缩进,这样你就不会错过大括号、结束语句等行尾。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多