我有2个id存储在数据库中。我没有碰到'spec'文件夹。但仍localhost:3000/users/1
显示此错误。
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 %>
模特 - &gt; 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+\-.]+@[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
答案 0 :(得分:-2)
编辑show.html.erb如下:
<%- if @user.present? %>
<%= @user.name %>, <%= @user.email %>
<%end%>
由于@user为零而出现问题。所以你应该检查用户的对象是否为零。然后根据您的要求处理对象。