Currently getting an undefined method and I am unsure how to fix this issue, been stuck on this for a while now. All help is much appreciated!
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",
User Controller
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
Comment Controller
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
Comment Form
<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>
I hate to tell you this but you have a typo. You typed
@micopost.comments.newinstead of@micropost.comments.new. Correct that and it’ll fix this problem.