ruby rails tutorial chapter 10 error

349 Views Asked by At

I'm getting 8 Errors in chapter 10 of Michael Hartls Ruby on Rails Tutorial. Everything worked just fine until the testing of 10.2.2 . I have no idea how to fix it... I hope you can help. Thanks in advance.

The error messages:

E

Error: UsersSignupTest#test_invalid_signup_information: SyntaxError: /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59: syntax error, unexpected keyword_end, expecting end-of-input test/integration/users_signup_test.rb:6:in `block in '

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

E

Error: UsersSignupTest#test_valid_signup_information: SyntaxError: /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59: syntax error, unexpected keyword_end, expecting end-of-input test/integration/users_signup_test.rb:17:in `block in '

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

...E

Error: UsersLoginTest#test_login_with_valid_information_followed_by_logout: SyntaxError: /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59: syntax error, unexpected keyword_end, expecting end-of-input test/integration/users_login_test.rb:24:in `block in '

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

E

Error: UsersEditTest#test_successful_edit: SyntaxError: /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59: syntax error, unexpected keyword_end, expecting end-of-input test/integration/users_edit_test.rb:22:in `block in '

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

E

Error: UsersEditTest#test_unsuccessful_edit: SyntaxError: /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59: syntax error, unexpected keyword_end, expecting end-of-input test/integration/users_edit_test.rb:11:in `block in '

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

......E

Error: UsersControllerTest#test_should_redirect_update_when_logged_in_as_wrong_user: SyntaxError: /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59: syntax error, unexpected keyword_end, expecting end-of-input test/controllers/users_controller_test.rb:23:in `block in '

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

E

Error: UsersControllerTest#test_should_get_new: SyntaxError: /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59: syntax error, unexpected keyword_end, expecting end-of-input test/controllers/users_controller_test.rb:11:in `block in '

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

E

Error: UsersControllerTest#test_should_redirect_edit_when_logged_in_as_wrong_user: SyntaxError: /home/studi/workspace/sample_app/app/controllers/users_controller.rb:59: syntax error, unexpected keyword_end, expecting end-of-input test/controllers/users_controller_test.rb:16:in `block in '

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

user_controller_test file:

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 file:

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

The error occured right after the instructions of 10.2.2. The testing of 10.2.1 worked fine.

2

There are 2 best solutions below

2
On BEST ANSWER

Looks like you've got an extra end in the users_controller, right before the closing one - line 58

Edit: what @SebastianPalma said - beat me to it :)

1
On

You have extra end in User#correct_user method. Try this:

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

You dint paid attention to difference between the syntax of if - modifier and if -statment.you dont have to use extra end at the end of modifier.these both are same without any syntax error.

if c.empty?
  return
end

return if c.empty?