ruby rails tutorial第10章错误

时间:2017-12-19 11:14:14

标签: ruby-on-rails ruby user-testing

我在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,期望输入结束       test / integration / users_signup_test.rb:6:在`block in'

中      

bin / rails test test / integration / users_signup_test.rb:5

     

电子

     

错误:UsersSignupTest#test_valid_signup_information:SyntaxError:   /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:   语法错误,意外的keyword_end,期望输入结束       test / integration / users_signup_test.rb:17:在`block in'

中      

bin / rails test test / integration / users_signup_test.rb:16

     

... E

     

错误:   UsersLoginTest#test_login_with_valid_information_followed_by_logout:   语法错误:   /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:   语法错误,意外的keyword_end,期望输入结束       test / integration / users_login_test.rb:24:在`block in'

中      

bin / rails test test / integration / users_login_test.rb:18

     

电子

     

错误:UsersEditTest#test_successful_edit:SyntaxError:   /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:   语法错误,意外的keyword_end,期望输入结束       test / integration / users_edit_test.rb:22:在'block in'中

     

bin / rails test test / integration / users_edit_test.rb:20

     

电子

     

错误:UsersEditTest #test_unsuccessful_edit:SyntaxError:   /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:   语法错误,意外的keyword_end,期望输入结束       test / integration / users_edit_test.rb:11:在`block in'

中      

bin / rails test test / integration / users_edit_test.rb:9

     

......ë

     

错误:   的UsersControllerTest#test_should_redirect_update_when_logged_in_as_wrong_user:   语法错误:   /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:   语法错误,意外的keyword_end,期望输入结束       test / controllers / users_controller_test.rb:23:在`block in'

中      

bin / rails test test / controllers / users_controller_test.rb:21

     

电子

     

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

中      

bin / rails test test / controllers / users_controller_test.rb:10

     

电子

     

错误:   的UsersControllerTest#test_should_redirect_edit_when_logged_in_as_wrong_user:   语法错误:   /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59:   语法错误,意外的keyword_end,期望输入结束       test / controllers / users_controller_test.rb:16:在`block in'

中      

bin / rails test test / controllers / 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的测试工作正常。

2 个答案:

答案 0 :(得分:1)

看来你在end之前有一个额外的users_controller,就在结束之前 - 第58行

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

答案 1 :(得分: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 - modifier和if -statment的语法之间的区别。你不必在modifier的末尾使用额外的结尾。这两个都没有任何语法错误。

if c.empty?
  return
end

return if c.empty?