1
votes

So as the title says, I'm following Michael Hartl's RoR Book and I'm currently on chapter 9.3 - Showing User. All tests passed before this chapter, I'm following the Rails 4 version of the book and I'm very new to this RoR.

I did bundle exec rspec spec/ and got this errors

1) Authentication authorization for non-signed-in users in the Users controller visiting the user index Failure/Error: it { should have_title('Sign in') } expected #has_title?("Sign in") to return true, got false # ./spec/requests/authentication_pages_spec.rb:80:in `block (6 levels) in '

2) User pages index Failure/Error: visit users_path ActionView::Template::Error: wrong number of arguments (2 for 1) # ./app/helpers/users_helper.rb:4:in gravatar_for' # ./app/views/users/index.html.erb:7:inblock in _app_views_users_index_html_erb__3697129218785207645_26541860' # ./app/views/users/index.html.erb:5:in _app_views_users_index_html_erb__3697129218785207645_26541860' # ./spec/requests/user_pages_spec.rb:12:inblock (3 levels) in '

3) User pages index Failure/Error: visit users_path ActionView::Template::Error: wrong number of arguments (2 for 1) # ./app/helpers/users_helper.rb:4:in gravatar_for' # ./app/views/users/index.html.erb:7:inblock in _app_views_users_index_html_erb__3697129218785207645_26541860' # ./app/views/users/index.html.erb:5:in _app_views_users_index_html_erb__3697129218785207645_26541860' # ./spec/requests/user_pages_spec.rb:12:inblock (3 levels) in '

4) User pages index should list each user Failure/Error: visit users_path ActionView::Template::Error: wrong number of arguments (2 for 1) # ./app/helpers/users_helper.rb:4:in gravatar_for' # ./app/views/users/index.html.erb:7:inblock in _app_views_users_index_html_erb__3697129218785207645_26541860' # ./app/views/users/index.html.erb:5:in _app_views_users_index_html_erb__3697129218785207645_26541860' # ./spec/requests/user_pages_spec.rb:12:inblock (3 levels) in '

Finished in 3.16 seconds 66 examples, 4 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:80 # Authentication authorization for non-signed-in users in the Users controller visiting the user index rspec ./spec/requests/user_pages_spec.rb:16 # User pages index rspec ./spec/requests/user_pages_spec.rb:15 # User pages index rspec ./spec/requests/user_pages_spec.rb:18 # User pages index should list each user

Randomized with seed 40709

Here's my authentication_pages_spec.rb

  require 'spec_helper'

  describe "Authentication" do

    subject { page }

    describe "signin page" do
      before { visit signin_path }

      it { should have_content('Sign in') }
      it { should have_title('Sign in') }
    end

    describe "signin" do
      before { visit signin_path }

      describe "with invalid information" do
        before { click_button "Sign in" }

        it { should have_title('Sign in') }
        it { should have_selector('div.alert.alert-error', text: 'Invalid') }

        describe "after visiting another page" do
          before { click_link "Home" }
          it { should_not have_selector('div.alert.alert-error') }
        end
      end

      describe "with valid information" do
        let(:user) { FactoryGirl.create(:user) }
        before { sign_in user }

        it { should have_title(user.name) }
        it { should have_link('Users',       href: users_path) }
        it { should have_link('Profile',     href: user_path(user)) }
        it { should have_link('Settings',    href: edit_user_path(user)) }
        it { should have_link('Sign out',    href: signout_path) }
        it { should_not have_link('Sign in', href: signin_path) }

        describe "followed by signout" do
          before { click_link "Sign out" }
          it { should have_link('Sign in') }
        end
      end
    end
    describe "authorization" do

      describe "for non-signed-in users" do
        let(:user) { FactoryGirl.create(:user) }
        describe "when attempting to visit a protected page" do
          before do
            visit edit_user_path(user)
            fill_in "Email",    with: user.email
            fill_in "Password", with: user.password
            click_button "Sign in"
          end

          describe "after signing in" do

            it "should render the desired protected page" do
              expect(page).to have_title('Edit user')
            end
          end
        end

        describe "in the Users controller" do

          describe "visiting the edit page" do
            before { visit edit_user_path(user) }
            it { should have_title('Sign in') }
          end

          describe "submitting to the update action" do
            before { patch user_path(user) }
            specify { expect(response).to redirect_to(signin_path) }
          end

          describe "visiting the user index" do
            before { visit users_path }
            it { should have_title('Sign in') }
          end
        end
      end

      describe "as wrong user" do
        let(:user) { FactoryGirl.create(:user) }
        let(:wrong_user) { FactoryGirl.create(:user, email: "[email protected]") }
        before { sign_in user, no_capybara: true }

        describe "submitting a GET request to the Users#edit action" do
          before { get edit_user_path(wrong_user) }
          specify { expect(response.body).not_to match(full_title('Edit user')) }
          specify { expect(response).to redirect_to(root_url) }
        end

        describe "submitting a PATCH request to the Users#update action" do
          before { patch user_path(wrong_user) }
          specify { expect(response).to redirect_to(root_url) }
        end
      end
    end
  end

And Here's my users_controller.rb

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

  def index
    @users = User.all
  end

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

  def new
    @user = User.new
  end

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

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

def create
    @user = User.new(user_params)
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  private

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

    # Before filters

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end

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

And here's my user_pages_spec.rb

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

  def index
  end

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

  def new
    @user = User.new
  end

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

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

def create
    @user = User.new(user_params)
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  private

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

    # Before filters

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end

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

I really have no idea what went wrong, please point me to the right direction.

2

2 Answers

0
votes

This looks like a template error.

One error points towards this file: /app/helpers/users_helper.rb line 4 and it looks like you have the wrong number of arguments in gravatar_for. Then the next place to look is /app/views/users/index.html.erb line 7.

0
votes

I figured it out.

The problem is with the gravatar_for arguments.

I made mistake and did not update the gravatar_for method in users_helper that's what is giving this error.

def gravatar_for(user, options = { size: 50 })
  gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
  size = options[:size]
  gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
  image_tag(gravatar_url, alt: user.name, class: "gravatar")
end

Updated gravatar_for method.