Dronny
Dronny

Reputation: 109

Ruby on Rails bundle exec rspec spec/ massive errors

I am quite new to coding and on chapter 8 of the Mhartl tutorial and ran into 34 errors when I tried to -bundle exec rspec spec/. Unfortunately, I can only make out errors 19-34(1-18 don't fit in my terminal).

Static pages About page it should behave like all static pages
<[31mFailure/Error:<-[0m <[31mbefore { visit root_path }<]0m
<[31mActionView:Template::Error<[0m:
  <[31mSQLite3::SQLException: no such column: users.remember_token: SELECT
"users".* FROM "users" WHERE "users"."remember_token" = 'lotsofrandomcharacters' LIMIT     1<[0m

That is error 19. The others are similar and refer to:

Static pages Help page
Static pages Help page it should behave like all static pages
Static pages Help page it should behave like all static pages
Static pages Home page
Static pages Home page it should behave like all static pages
Static pages Home page it should behave like all static pages
Static pages Contact page
Static pages Contact page it should behave like all static pages
Static pages Contact page it should behave like all static pages
User
User remember token remember_token
User return value of authenticate method with valid password
User return value of authenticate method with valid password
User when email address is already taken

I am a programming noob and this is quite overwhelming to take in.

Here are a couple of the files I worked on in the chapter

app/views/layouts/_header.html.erb

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <%= link_to "sample app", root_path, id: "logo" %>
      <nav>
        <ul class="nav pull-right">
          <li><%= link_to "Home", root_path %></li>
          <li><%= link_to "Help", help_path %></li>
          <% if signed_in? %>
            <li><%= link_to "Users", '#' %></li>
            <li id="fat-menu" class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                Account <b class="caret"></b>
              </a>
              <ul class="dropdown-menu">
                <li><%= link_to "Profile", current_user %></li>
                <li><%= link_to "Settings", '#' %></li>
                <li class="divider"></li>
                <li>
                  <%= link_to "Sign out", signout_path, method: "delete" %>
                </li>
              </ul>
            </li>
          <% else %>
            <li><%= link_to "Sign in", signin_path %></li>
          <% end %>
        </ul>
      </nav>
    </div>
  </div>
</header>

app/helpers/sessions_helper.rb

module SessionsHelper

  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    remember_token = User.encrypt(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: remember_token)
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end
end

When I run the rails server, and try to view it on localhost:3000, I get :

sample_app/app/views/layouts/_header.html.erb where line #9 raised:

SQLite3::SQLException: no such column: users.remember_token: SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'lotsofcharacters' LIMIT 1

this line refers to: <% if signed_in? %>

I'm honestly not sure what other file to list here, since I can't even read the full error. I believe my contact/about/home pages are fine since they have remained unchanged in this chapter but if someone think I should list those as well I can do that. Thanks for the help!

Upvotes: 0

Views: 459

Answers (4)

user3613073
user3613073

Reputation: 11

I met the same issue while learning Mhartl tutorial. This problem shows up because when your database already have some user, then "remember_token" migrate won't work. You must clean your data first,then do db:migrate.like this:

rake db:drop 
db:create 
rake db:migrate

(be carefull: this will erase all your data)

Upvotes: 1

Alan Smith
Alan Smith

Reputation: 99

I don't know if you already resolved this, but I just worked through it myself. For some reason '$ rails generate migration add_remember_token_to_users' leaves an empty shell, so I had to go to db/migrate/[ts]_add_remember_token_to_users.rb and manually add:

add_column :users, :remember_token, :string
add_index  :users, :remember_token

to the change method. Then run:

$ bundle exec rake db:migrate

$ bundle exec rake test:prepare

And that should take care of the 'SQLite3::SQLException: no such column: users.remember_token: SELECT "users".* FROM "users" WHERE "users"."remember_token"...' error you were having.

Upvotes: 0

kristinalim
kristinalim

Reputation: 3459

The error gives a hint that your DB was not set up properly:

SQLite3::SQLException: no such column: users.remember_token: SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'lotsofcharacters' LIMIT 1

That says that you have a missing column. The definition of that column is probably in one of your DB migration files.

Run bundle exec rake db:migrate (for development) and bundle exec rake db:migrate RAILS_ENV=test (for test) to run the DB migrations that haven't been run before, and then try again.

Update:

DB migrations should be incremental, but in case the ones for your app were not written that way, you can look into these:

  1. Check your DB migration scripts if there is indeed a line adding the missing column.
  2. Check the original copy of the DB schema file if there is indeed a line for the missing column.
  3. See @Jeremy's answer.

Upvotes: 1

whitehat101
whitehat101

Reputation: 2549

SQLite3::SQLException: no such column: users.remember_token:

That sounds like you haven't run rake db:migrate. Then run rake db:test:prepare to use the schema.rb (that db:migrate generates) to generate your testing db.

See this rails guide for a little more info.

Upvotes: 0

Related Questions