Ruby on rails Rails:用户中的NoMethodError#show
我是Rails的新手,一直漫无目的地在stackoverflow周围徘徊,试图找到问题的解决方案,但似乎无法解决。我正在学习Michael Hartl教程的第10章,当我试图查看特定用户的配置文件localhost:3000页面时,我会收到以下错误消息:Ruby on rails Rails:用户中的NoMethodError#show,ruby-on-rails,Ruby On Rails,我是Rails的新手,一直漫无目的地在stackoverflow周围徘徊,试图找到问题的解决方案,但似乎无法解决。我正在学习Michael Hartl教程的第10章,当我试图查看特定用户的配置文件localhost:3000页面时,我会收到以下错误消息: "NoMethodError in Users#show" 接 "undefined method `name' for nil:NilClass". 源代码列在show.html.erb文件的第一行,但我看不出代码有任何错误 主页运行
"NoMethodError in Users#show"
接
"undefined method `name' for nil:NilClass".
源代码列在show.html.erb文件的第一行,但我看不出代码有任何错误
主页运行良好,用户索引可查看,但除此之外,它不起作用。我理解这可能意味着@user对象为nil,但我不确定如何修复它。我的Rspec测试也很难通过-任何帮助都将不胜感激
我的用户\u controller.rb文件:
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update, :destroy]
# Arranges for a particular method to be called before the given actions.
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy # Restricts the destroy action to admins.
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def index
@users = User.paginate(page: params[:page])
end
def edit
# @user = User.find(params[:id])
end
def update
# @user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
private
# def signed_in_user
# unless signed_in?
# store_location
# redirect_to signin_url, notice: "Please sign in."
# end
# end
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
def admin_user
redirect_to(root_path) unless current_user.admin?
end
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
end
end
这里@user是零。因为没有id为(params[:id])的记录。这就是为什么会出现这个错误
<h1>
<%= gravatar_for @user %>
<%= !@user.nil? @user.name : "" %>
</h1>
It will check whether the @user having any record, if its having it will display it's name else it will display empty.
它将检查@user是否有任何记录,如果有,它将显示其名称,否则它将显示为空。
您的查询未找到用户:
@user = User.find(params[:id])
显然返回nil
请检查具有给定ID的用户是否存在。此外,当找不到任何对象时,应确保操作安全失败:
<% if @user.nil? %>
<% provide(:title, 'Not found') %>
No user with that ID has been found.
<% else %>
<!-- Contents of your current show.html.erb -->
<% end %>
找不到具有该ID的用户。
为什么在控制器中将show action作为私有方法?非常感谢Khaled!我不知道为什么我把表演动作放在那里-愚蠢-我只是把它添加到序列的末尾。刚刚把它移出私有方法,现在测试通过了,谢谢!呸…一直在为这件事苦恼…太棒了。我们可以用AND和ANDalso@vijikumar:NoMethodError将在多个位置发生,您的修复仅对其中一个位置有帮助。Yes@Koraktor。你是对的。。我刚刚给出了当前错误的解决方案。我正在通过用户索引导航到用户配置文件页面-因此单击现有用户。如果查询找不到用户,那么如何向现有用户发出警报?感谢所有回复!只是一个愚蠢的错误。解决了的!
@user = User.find(params[:id])
<% if @user.nil? %>
<% provide(:title, 'Not found') %>
No user with that ID has been found.
<% else %>
<!-- Contents of your current show.html.erb -->
<% end %>