irb just has the cached version of the object in memory. what does user.reload
instead of user
show you
creating an authenticated user in rails console is showing contradicting data
-
01-07-2022 - |
Вопрос
I'm reviewing Hartl's Rails Tutorial and while I'm able to successfully create a user in the console, I am confused by the current feedback:
2.0.0-p247 :014 > user.update_attributes name: "Ryan Waits", email: "ryan.waits@gmail.com"
(0.3ms) begin transaction
User Exists (0.3ms) # commented out
=> true
2.0.0-p247 :015 > user
=> #<User id: 2, name: nil, email: nil, # commented out
2.0.0-p247 :016 > user.name
=> "Ryan Waits"
It creates the user as it should, but why do I need to specifically call on user.name or user.email in order to show their values...anyone have an idea?
user model:
class User < ActiveRecord::Base
before_save { self.email == email.downcase }
attr_accessor :name, :email
validates :name, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false}
has_secure_password
validates :password, length: { minimum: 6 }
end
users controller:
class UsersController < ApplicationController
def index
@user = User.all
end
def new
end
def show
@user = User.find params[:id]
end
end
Besides the console showing a user has been created, I don't have any users in the database when trying to display them on either the index or show views.
index error:
undefined method `email' for #
index view:
<%= @user.name %>
<%= @user.email %>
Решение
Не связан с StackOverflow