Creating a new user with rails was angry with unknown attribute'password' for User.

While creating a web service that I thought about for the first time with rails, I suddenly got angry when creating a new user

unknown attribute 'password' for User.
              
  def create
    @user = User.new(
      name: params[:name],
      email: params[:email],
      password: params[:password]

Cause investigation

I was told that there is no password column, so I will check the columns in the Users table.

$ rails console
$ User.new
Loading development environment (Rails 6.0.3.1)
irb(main):001:0> User.new
   (7.5ms)  SELECT sqlite_version(*)
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>

Certainly there is no password column. The password column should have been created in the users table by creating a migration file, but ... Let's take a look at the migration file

class AddPasswordToUsers < ActiveRecord::Migration[6.0]
  def change
    add_column: :users, :password, :string
  end
end

There was an extra colon after add_column

Conclusion

I corrected the code mistake and rails db: migrate and it worked. It was a rudimentary mistake so I want to be careful

Recommended Posts

Creating a new user with rails was angry with unknown attribute'password' for User.
[Rails] Creating a new project with rails new
Naming convention when creating a new controller or model with rails
[Rails6] Create a new app with Rails [Beginner]
Rails Basics of creating a new application
[Rails 5] Create a new app with Rails [Beginner]
[Rails] rails new to create a database with PostgreSQL
[For beginners] Procedure for creating a controller using rails
How to get started with creating a Rails app
Tutorial to create a blog with Rails for beginners Part 1
Tutorial to create a blog with Rails for beginners Part 0
Building an environment for creating apps with Rails and Vue
[Rails] Launch a new project
[Rails] Creating a search box
Preparation for developing with Rails
[Rails] Avoid SELECT issued when creating a model with belongs_to defined!
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
(For myself) Try creating a C # environment with docker + code-server, cloud9
Create a new app in Rails
[Ruby on Rails] Create a pie chart for each column with Chartkick
Creating a java web application development environment with docker for mac part1
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)