[英]Ruby on Rails: undefined method “comments” for nil:NilClass
I am developing an app with users where they each have set of microposts displayed on their pages. 我正在与用户一起开发一个应用程序,他们每个人的页面上都显示了一组微博。 I am trying to add comments to these microposts. 我正在尝试向这些微博添加评论。 Every time I visit localhost:3000/users/(user_id_#) 每次我访问localhost:3000 / users /(user_id_#)
I get this error: undefined method `comments' for nil:NilClass 我收到此错误:nil:NilClass的未定义方法“ comments”
This error only comes when the user has microposts to show. 仅当用户有微博显示时才会出现此错误。 Otherwise it just shows their blank page. 否则,它将仅显示其空白页。 The error comes from this view for app/views/users/show.html.erb This view renders this partial, where the error occurs in line 13. 错误来自此视图的app / views / users / show.html.erb。此视图呈现此部分错误,该错误发生在第13行。
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %>
<% end %>
<h2>Comments</h2>
<% micropost.comments.each do |comment| %>
<p>
<b>Commenter:</b>
<%= comment.commenter %>
</p>
<p>
<b>Comment:</b>
<%= comment.body %>
</p>
<% end %>
<h3>Add a comment:</h3>
<%= form_for([micropost, micropost.comments.build]) do |f| %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</li>
Here is my comment.rb file 这是我的comment.rb文件
class Comment < ActiveRecord::Base
belongs_to :micropost
attr_accessible :body, :user_id
end
and my micropost.rb file 和我的micropost.rb文件
class Micropost < ActiveRecord::Base
attr_accessible :content
belongs_to :user
has_many :comments
validates :content, presence: true, length: { maximum: 140 }
validates :user_id, presence: true
end
and my comments_controller.rb 和我的comments_controller.rb
class CommentsController < ApplicationController
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.create(params[:comment])
redirect_to micropost_path(@micropost)
end
end
and finally my microposts_controller.rb 最后是我的microposts_controller.rb
class MicropostsController < ApplicationController
before_filter :signed_in_user
before_filter :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_path
else
@feed_items = []
render 'static_pages/home'
end
end
def new
@micropost = Micropost.new(params[:micropost])
end
def show
@micropost = Micropost.find(params[:id])
end
def destroy
@micropost.destroy
redirect_back_or root_path
end
private
def correct_user
@micropost = current_user.microposts.find_by_id(params[:id])
redirect_to root_path if @micropost.nil?
end
end
class CommentsController < ApplicationController
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.create(params[:comment])
redirect_to micropost_path(@micropost)
end
end
also here is the users_controller.rb 也是这里的users_controller.rb
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])
@micropost = @user.microposts.first
@comment = Comment.new
end
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 edit
end
def update
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_path
end
def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.followed_users.paginate(page: params[:page])
render 'show_follow'
end
def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
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
from the error it sounds like the @micropost isnt being initialized in the microposts_controller.rb file under def show. 从错误中听起来像@micropost未在def show下的microposts_controller.rb文件中初始化。 but I think it is? 但我认为是吗? What am I doing wrong? 我究竟做错了什么? Thanks 谢谢
also here is app/views/users/show.html.erb 也在这里是app / views / users / show.html.erb
<% provide(:title, @user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</section>
</aside>
<div class="span8">
<%= render 'follow_form' if signed_in? %>
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
<%= render @microposts %>
</ol>
<%= will_paginate @microposts %>
<% end %>
</div>
</div>
I suspect the error is here in your partial: 我怀疑您的部分错误在这里:
<% @micropost.comments.each do |comment| %>
I am assuming that you are iterating over @microposts and passing each micro post to your partial as micropost . 我假设您正在遍历@microposts并将每个微博传递给您的部分微 博 。 In the above line you are using the instance variable @micropost when you should be using the local micropost . 在上一行中,当您应该使用本地micropost时,您正在使用实例变量@micropost 。
Update: and also here: 更新:以及这里:
<%= form_for([@micropost, @micropost.comments.build]) do |f| %> –
Could you try to change this @microposts = @user.microposts.paginate(page: params[:page])
into this @micropost = @user.microposts.first
and see if it works ? 您能否尝试将此@microposts = @user.microposts.paginate(page: params[:page])
@micropost = @user.microposts.first
为此@micropost = @user.microposts.first
,看看是否@micropost = @user.microposts.first
? I think since the partial is rendered in the context of users#show that why @micropost is nil. 我认为由于部分内容是在用户的上下文中呈现的,因此表明@micropost为什么为零。
This is not an answer, I just want to comment but I don't have enough reputation points. 这不是答案,我只是想发表评论,但我的信誉点不够。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.