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 -
- polymorphic ActiveRecord::UnknownAttributeError
heroku - I'm not using kerokuapp https://stackguides.com/questions/8139578/activerecordunknownattributeerror
Failures:
1) SessionsController post create logs you in with the correct password Failure/Error: let(:connie) {User.create(name: 'Connie', password: 'M4heswaran')} ActiveRecord::UnknownAttributeError: unknown attribute 'name' for User.
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
bin/rake db:migrate RAILS_ENV=development
, and that helped clear the unknown attribute error! Thanks @Iceman @Anton – user7559874