目前正在获取一个未定义的方法,我不确定如何解决这个问题,现在已经停留了一段时间。非常感谢所有帮助!
NoMethodError in Users#show
undefined method `comments' for nil:NilClass
Extracted source (around line #2):
1: <div class="CommentField">
2: <%= form_for ([@micropost, @micopost.comments.new]) do |f| %>
3: <%= f.text_area :content, :class => "CommentText", :placeholder => "Write a Comment..." %>
4: <div class="CommentButtonContainer">
5: <%= f.submit "Comment",
用户控制器
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@school = School.find(params[:id])
@micropost = Micropost.new
@comment = Comment.new
@comment = @micropost.comments.build(params[:comment])
@microposts = @user.microposts.paginate(:per_page => 10, :page => params[:page])
end
end
评论控制器
class CommentsController < ApplicationController
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.build(params[:comment])
@comment.user_id = current_user.id
@comment.save
respond_to do |format|
format.html
format.js
end
end
end
评论表
<div class="CommentField">
<%= form_for ([@micropost, @micopost.comments.new]) do |f| %>
<%= f.text_area :content, :class => "CommentText", :placeholder => "Write a Comment..." %>
<div class="CommentButtonContainer">
<%= f.submit "Comment", :class => "CommentButton b1" %>
</div>
<% end %>
</div>
答案 0 :(得分:2)
我不想告诉你这个,但你有一个错字。您输入了@micopost.comments.new
而不是@micropost.comments.new
。纠正这一点,它将解决这个问题。