我有2個ID存儲在數據庫中。我沒有碰過'spec'文件夾。但仍然localhost:3000/users/1
顯示此錯誤。未定義的方法名稱爲零:NilClass
undefined method `name' for nil:NilClass
Extracted source (around line #1):
1: <%= @user.name %>, <%= @user.email %>
users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
end
end
show.html.erb
<%= @user.name %>, <%= @user.email %>
車型 - > users.rb的
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end
是你的路由是否正確? '資源用戶' –
檢查你的數據庫是否有'用戶'與'id = 1'。你也可以用應用程序日誌更新'show'動作的問題。 –
請粘貼您的應用程序日誌。 –