【问题标题】:Rails: Undefined Method Issue [closed]Rails:未定义的方法问题[关闭]
【发布时间】:2012-03-16 14:59:45
【问题描述】:

目前得到一个未定义的方法,我不确定如何解决这个问题,现在被困了一段时间。非常感谢所有帮助!

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>

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 methods undefined


    【解决方案1】:

    我不想告诉你这个,但你有一个错字。您输入了 @micopost.comments.new 而不是 @micropost.comments.new。纠正它,它会解决这个问题。

    【讨论】:

    • 该死的代表我的愚蠢举动,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2012-06-25
    • 2018-10-15
    • 2016-10-14
    • 1970-01-01
    • 2019-07-25
    相关资源
    最近更新 更多