ruby-on-rails - 错误 ActionView::Template::Error (nil:NilClass 的未定义方法“名称”)
问题描述
我正在使用 ruby 2.4 和 rails 5.1.6。
为了学习 RoR,我手工制作了一个用户模型、视图和控制器,而不是使用脚手架。
填写表单以创建新用户后,我在 rails 服务器日志中收到此错误:
Started POST "/users" for 127.0.0.1 at 2018-08-27 15:52:45 +0530
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"gdSumxEvNi6ECKBZLFKN9dB/PYLOmixMcf5oYcUGkE1u4iAa/YkJlu7SJbPQNlh6J2SEzR2WeSUT9frhteEOCw==", "user"=>{"name"=>"User1", "email"=>"user1@yahoo.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign Up"}
(0.2ms) begin transaction
User Exists (0.6ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) LIMIT ? [["email", "user1@yahoo.com"], ["LIMIT", 1]]
SQL (0.8ms) INSERT INTO "users" ("name", "email", "password_digest", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["name", "User1"], ["email", "user1@yahoo.com"], ["password_digest", "$2a$10$Efhs2Z2BRa5FCUdSIz6D5.c/eiFQm3JaW818veE644NdoWluRLjoq"], ["created_at", "2018-08-27 10:22:45.636574"], ["updated_at", "2018-08-27 10:22:45.636574"]]
(1.0ms) commit transaction
Redirected to http://127.0.0.1:3000/users/1
Completed 302 Found in 93ms (ActiveRecord: 2.6ms)
Started GET "/users/1" for 127.0.0.1 at 2018-08-27 15:52:45 +0530
Processing by UsersController#show as HTML
Parameters: {"id"=>"1"}
Rendering users/show.html.erb within layouts/application
Rendered users/show.html.erb within layouts/application (9.8ms)
Completed 500 Internal Server Error in 32ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (undefined method `name' for nil:NilClass):
1:
2: <h1><%= @user.name %></h1>
3: <h2><%= @user.email %></h2>
app/views/users/show.html.erb:2:in `_app_views_users_show_html_erb___1408287096995644504_70277415453360'
如您所见,记录已成功保存到数据库,但不知何故,@user 变量在到达 show.html.erb 模板时被销毁。
这是 users_controller.rb
class UsersController < ApplicationController
def index
end
def show
byebug
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to @user
byebug
else
render 'new'
end
end
def edit
end
def update
end
def destroy
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
...这里是 show.html.erb 的内容
<h1><%= @user.name %></h1>
<h2><%= @user.email %></h2>
在 user#create 操作中的 byebug 断点处 @user.name 的输出显示了正确的值,但是在 user#show 操作中的第二个 byebug 断点处,我收到此错误:
(byebug) @user.name
*** NoMethodError Exception: undefined method `name' for nil:NilClass
为了确保,我制作了第二个应用程序,但使用了脚手架生成器,一切正常。
有任何想法吗?
ps
我在用户模型(app/models/user.rb)中写了一些验证。我在下面包含了这个以防万一......
class User < ApplicationRecord
validates :name, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
before_save {email.downcase!}
validates :email, presence: true,
format: {with: VALID_EMAIL_REGEX},
uniqueness: {case_sensitive: false}
validates :password, presence: true, length: {minimum: 6}
has_secure_password
end
解决方案
我想你还没有定义@user
对象。您可以通过@user = User.find_by(id: params[:id])
在您的show
操作中添加来做到这一点
推荐阅读
- javascript - How to pass event into lamda function
- c++ - 如何在 C++ 中从类更改为结构?
- python - 扩展 QTreeWidgetItem 中的消失数据
- r - 重写 data.frame 并将值替换为第二个 data.frame 中最接近的值
- mysql - Mysql -h 选项不起作用
- c++ - 编写一个将参数输入到 std::bind 对象向量中的函数
- python - python-lzw 不解压缩较大的 blob
- python - opname 如何/在哪里填充?
- amazon-s3 - 使用邮递员的 AWS S3 预签名 URL 上传图像
- javascript - Reactjs - 列表中自定义组件的键