0

我在 Michael Hartls Ruby on Rails 教程的第 10 章中遇到 8 个错误。在测试 10.2.2 之前一切正常。我不知道如何解决它......我希望你能提供帮助。提前致谢。

错误消息:

错误:UsersSignupTest#test_invalid_signup_information:SyntaxError:/home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:语法错误,意外keyword_end,预期输入结束测试/集成/users_signup_test.rb:6:in `阻止'

bin/rails 测试测试/集成/users_signup_test.rb:5

错误:UsersSignupTest#test_valid_signup_information:SyntaxError:/home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:语法错误,意外keyword_end,期望输入结束测试/集成/users_signup_test.rb:17:in `阻止'

bin/rails 测试测试/集成/users_signup_test.rb:16

...E

错误:UsersLoginTest#test_login_with_valid_information_followed_by_logout:SyntaxError:/home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:语法错误,意外keyword_end,期望输入结束测试/集成/users_login_test.rb:24:in `阻止'

bin/rails 测试测试/集成/users_login_test.rb:18

错误:UsersEditTest#test_successful_edit:SyntaxError:/home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:语法错误,意外keyword_end,期望输入结束测试/集成/users_edit_test.rb:22:in `阻止'

bin/rails 测试测试/集成/users_edit_test.rb:20

错误:UsersEditTest#test_unsuccessful_edit:SyntaxError:/home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:语法错误,意外keyword_end,期望输入结束测试/集成/users_edit_test.rb:11:in `阻止'

bin/rails 测试测试/集成/users_edit_test.rb:9

......E

错误:UsersControllerTest#test_should_redirect_update_when_logged_in_as_wrong_user:SyntaxError:/home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:语法错误,意外keyword_end,期待输入结束测试/controllers/users_controller_test.rb:23:in `阻止'

bin/rails 测试测试/控制器/users_controller_test.rb:21

错误:UsersControllerTest#test_should_get_new:SyntaxError:/home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:语法错误,意外keyword_end,期望输入结束测试/controllers/users_controller_test.rb:11:in `阻止'

bin/rails 测试测试/控制器/users_controller_test.rb:10

错误:UsersControllerTest#test_should_redirect_edit_when_logged_in_as_wrong_user:SyntaxError:/home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:语法错误,意外的keyword_end,期待输入结束测试/controllers/users_controller_test.rb:16:in `阻止'

bin/rails 测试测试/控制器/users_controller_test.rb:14

user_controller_test 文件:

require 'test_helper'

class UsersControllerTest < ActionDispatch::IntegrationTest

  def setup
  @user = users(:michael)
  @other_user = users(:archer)
end

  test "should get new" do
    get signup_path
    assert_response :success
  end
  test "should redirect edit when logged in as wrong user" do
    log_in_as(@other_user)
    get edit_user_path(@user)
    assert flash.empty?
    assert_redirected_to root_url
  end

  test "should redirect update when logged in as wrong user" do
    log_in_as(@other_user)
    patch user_path(@user), params: { user: { name: @user.name,
                                              email: @user.email } }
    assert flash.empty?
    assert_redirected_to root_url
  end
end

users_controller 文件:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:edit, :update]
  before_action :correct_user,   only: [:edit, :update]

  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 to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
      if @user.update_attributes(user_params)
        flash[:success] = "Profile updated"
        redirect_to @user
      else
        render 'edit'
      end
    end

  private

  def user_params
        params.require(:user).permit(:name, :email, :password,
                                     :password_confirmation)
      end
    # Before filters

    # Confirms a logged-in user.
    def logged_in_user
      unless logged_in?
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

    # Confirms the correct user.
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless @user == current_user
    end
    end
end

错误发生在 10.2.2 的说明之后。10.2.1 的测试运行良好。

4

2 回答 2

1

看起来你有一个额外endusers_controller,就在最后一行 58 之前

编辑:@SebastianPalma 说的 - 打败我 :)

于 2017-12-19T11:17:26.040 回答
0

您在 User#correct_user 方法中有额外的结尾。试试这个:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:edit, :update]
  before_action :correct_user,   only: [:edit, :update]

  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 to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :email, :password,
                                     :password_confirmation)
  end
  # Before filters

  # Confirms a logged-in user.
  def logged_in_user
    unless logged_in?
      flash[:danger] = "Please log in."
      redirect_to login_url
    end
  end

  # Confirms the correct user.
  def correct_user
    @user = User.find(params[:id])
    redirect_to(root_url) unless @user == current_user
  end
end

您注意到 if - 修饰符和 if -statment 的语法之间的区别。您不必在修饰符的末尾使用额外的 end。它们都是相同的,没有任何语法错误。

if c.empty?
  return
end

return if c.empty?
于 2017-12-19T11:27:43.017 回答