我查看了许多其他线程并花了过去几个小时查看它,但我仍然无法弄清楚。
我正在使用来自另一个视图页面的同一行代码
这是错误:
app/views/users/_user.html.erb
其中第 4 行提出:
undefined method `microposts' for nil:NilClass
如果您熟悉的话,这是来自 Rubytutorial 演示应用程序。使用了此代码views\users\show.html.erb
,我尝试查看它是否可以正常工作,views\users\ _user.html.erb
但出现错误。
我重用的代码是:
<%= @user.microposts.count %>
这是调用部分的完整视图(_user.html.erb)
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
<%- if @user %>
<%= @user.microposts.count %>
<%- else %>
THIS IS THE ERROR, THERE IS NO USER ON @user
<%- end %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete, confirm: "You sure?" %>
<% end %>
</li>
这导致这是错误,\@user 上没有用户。这意味着根据 rorra 的评论,在部分情况下,\@user 不存在,那么我在哪里解决这个问题?
class UsersController < ApplicationController
before_filter :signed_in_user,
only: [:index, :edit, :update, :destroy, :following, :followers]
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to Sample App!"
redirect_to root_url
else
render 'new'
end
end
def edit
end
def update
if @user.update_attributes(params[:user])
sign_in @user
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed"
redirect_to users_path
end
private
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
end
这是views\users\index.html.erb
<% provide(:title, 'Users') %>
<h1>Users</h1>
<%= will_paginate %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>
这是views\users\show.html.erb
<% provide(:title, @user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</section>
<section>
<%= render 'shared/stats' %>
</section>
<br>
</aside>
<div class="span10">
<%= render 'follow_form' if signed_in? %>
<% if @user.microposts.empty? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<% end %>
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
<%= render @microposts %>
</ol>
<%= will_paginate @microposts %>
<% end %>
</div>
</div>