1

I am new to Ruby on Rails, and I am coming across an UnknownAttributeError through my rspec tests. Where and how do you debug this issue? Thanks in advance.

I've looked through these questions, but my problem is NOT -

I am creating a login page with name, password, and password confirmation, along with a submit button by using form_for.

<h1>Welcome!</h1>
<h3>Please log in, stranger.</h3>
<%= form_for @user do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.label :password %>
  <%= f.text_field :password %>

  <%= f.label :password_confirmation %>
  <%= f.text_field :password_confirmation %>

  <%= f.submit %>
<% end %>

I also have a Sessions controller with actions that allows a session to be created when a user logs in:

class SessionsController < ApplicationController
  def new
  end

  def create
    @user = User.find_by(name: params[:user][:name])
  end
end

Here is my users controller, just in case, so that I'm not leaving anything out

class UsersController < ApplicationController
  def new
  end

  def create
    user = User.new(user_params).save
  end

  private

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

"name" is an attribute in my users table. Here is my schema below:

ActiveRecord::Schema.define(version: 20170416184846) do

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "password_digest"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
  end
end

And I only have a ActionController class method before_action which shouldn't affect the unknown attribute error.

class User < ActiveRecord::Base
  has_secure_password
end

I also created a record in rails c, and I successfully saved one.

2.3.0 :002 > User.create(name: "Dave", password: "dave", password_confirmation: "dave")

(0.1ms) begin transaction SQL (0.5ms) INSERT INTO "users" ("name", "password_digest", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Dave"], ["password_digest", "$2a$10$bq9G.od4ecoTRWg0dlwREudjdDX5QFa7L.c1U2Jd6qoKmCnGNG61O"], ["created_at", "2017-04-16 20:41:04.061507"], ["updated_at", "2017-04-16 20:41:04.061507"]] (0.8ms) commit transaction => #

And finally, here are my routes:

Rails.application.routes.draw do
  get '/login', to: 'sessions#new'
  post '/login', to: 'sessions#create'
  delete '/logout', to: 'sessions#destroy'
end

Here is the rspec file for SessionsController:

require 'rails_helper'

RSpec.describe SessionsController, type: :controller do

  before do
    User.destroy_all
  end

  let(:connie) {User.create(name: 'Connie', password: 'M4heswaran')}

  describe 'post create' do
    it 'logs you in with the correct password' do
      post :create, user: {name: connie.name, password: connie.password}
      expect(session[:user_id]).to eq(connie.id)
    end

    it 'rejects invalid passwords' do
      post :create, user: {name: connie.name, password: connie.password + 'x'}
      expect(session[:user_id]).to be_nil
    end

    it 'rejects empty passwords' do
      post :create, user: {name: connie.name, password: ''}
      expect(session[:user_id]).to be_nil
    end
  end
end
Community
  • 1
  • 1
  • Where do you see that message @Iceman? –  Apr 16 '17 at 20:44
  • Could you add the entire sessions_controller_spec? – Anton Apr 16 '17 at 20:46
  • Sure, I just updated the question with spec file for SessionsController @Anton –  Apr 16 '17 at 20:55
  • Do puts connie at the top of your test and check out the log. Is there a valid object? Why don't you do user: connie instead of the hash? – Anton Apr 16 '17 at 21:02
  • I got "connie", and my tests ran the same afterwards. I actually ran `bin/rake db:migrate RAILS_ENV=development`, and that helped clear the unknown attribute error! Thanks @Iceman @Anton –  Apr 16 '17 at 21:09
  • @Iceman I'd encourage you to put your answer up so that user112514 can accept your answer as the correct one. It might be useful to direct her to the appropriate point in the rails guides – Julian Leviston Apr 17 '17 at 00:29

2 Answers2

6

You have to set up your test database also with this command from the terminal

rails db:test:prepare

One other way would be

RAILS_ENV=test rails db:migrate 

Can read more about this all in the excellent guides

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
0

If you have just started and no data in your database start from fresh

Drop the database:

rake db:drop

Create all database:

rake db:create

prepare your test db:

rake db:test:prepare

migrate your db:

rake db:migrate

then migrate your db for test env:

rake db:migrate RAILS_ENV=test
nitanshu verma
  • 261
  • 3
  • 8