Unable to create User in Rails Tutorial Chapter 6

Asked 2 years ago, Updated 2 years ago, 41 views

We are working on Chapter 6 of Rails Tutorial (Japanese translation).

Looking at the console, it looks like a user has been created.

Couldn't find User with id=1

Please tell me the reason why you throw up the error.

user_controller.rb

class UsersController<ApplicationController

  def show

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

  end

  def new

  end

end

user.rb

class User<ActiveRecord::Base

  before_save {self.email=email.downcase}

  values:name,presence:true,length:{maximum:50}
  VALID_EMAIL_REGEX=/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates:email, presence:true,format:{with:VALID_EMAIL_REGEX},
                    unity: {case_sensitive:false}

  has_secure_password
  values:password,length: {minimum:6}

end

rails console --sandbox Results

2.0.0-p481:007>User.find(1)
  User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users". "id" =?LIMIT1 [[["id", 2]]
 =>#<User id:2,name:"testuesr",email:"[email protected]",created_at:"2015-01-01 13:28:14",updated_at:"2015-01-01 13:28:14",password_digest:"$2a$10$XHiVgRQrbx8oehDGi.Ko/gny6NHDGi... 

user table

class CreateUsers <ActiveRecord::Migration
  def change
    create_table —users do | t |
      t.string —name
      t.string —email

      t.timestamps

    end

  end

end

schema.rb

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

  create_table "users", force: true do | t |
    t.string "name"
    t.string "email"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string "password_digest"
  end

  add_index "users", ["email", name: "index_users_on_email", unique: true

end

ruby ruby-on-rails

2022-09-30 19:27

2 Answers

This may be because you are creating a user on the sandbox console.

Launch the console with rails console and create a user, as described in Section 6.3.5, and you should be able to see it on your browser.

When you launch the console with --sandbox, all DB writes are done in the transaction, so you can't see any changes from outside the console.Also, all changes will be canceled when you exit the console.(Related Source Code)


2022-09-30 19:27

The registered user id is 2.

 bundle exec rakedb:migrate

After returning the DB state to its initial state, use irb (or pry again) to

user=User.new(name: "Michael Hartl", email: "[email protected]")
user.save

and so on.


2022-09-30 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.