1
現在、未定義のメソッドを取得していますが、この問題を修正する方法が不明ですが、しばらくこれに取り組んでいます。すべての助けが大変ありがとう!Rails:Undefined Method Issue
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>
godammit愚かな動きmybehalfで、ありがとう – Kellogs