1

私はacts_as_commentable_with_threading gemを使用しています。

私の _new.html.erb

<div class="adding_comment">&nbsp;</div>
<%=form_for [commentable, Comment.new], :remote => false do |f|%>
 <%=f.hidden_field :commentable_id, :value=> @post.id %><br/>
 <%=f.label :title%> :<br/>
 <%=f.text_field :title%><br/>
 <%=f.label :subject%> :<br/>
 <%=f.text_field :subject%><br/>
 <%=f.label :body%><br/>
 <%=f.text_area :body, :style=>"width:320;height:80px"%><br/><br/>
 <%=f.submit "Add comment"%>
<%end%>

コメント コントローラー:

class CommentsController < ApplicationController
 def create
  @post = Post.find(params[:comment][:commentable_id])
  @user_who_commented = current_user
  @comment = Comment.build_from(@post, @user_who_commented.id, "Comment!" )
 end
end

私の投稿モデルに追加しました:

acts_as_commentable

私の投稿の show.html.erb で:

<%= render :partial => "comments/new", :locals => { :commentable => @post }%><br/>
<h1>Comments</h1>
<div id="comments">
<%=render :partial => 'comments/index',:locals => {:commentable=> @post, :comments => @comments}%>
</div>

しかし、「送信」ボタンをクリックした後、エラーが発生します:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Request:
    {"utf8"=>"✓",
 "authenticity_token"=>"2GvCfaCB/qAxdh+1jJvcZ76jL9fdxP+h5qEIOClmNHk=",
 "comment"=>{"commentable_id"=>"1",
 "title"=>"et",
 "subject"=>"et",
 "body"=>"ewt"},
 "commit"=>"Add comment",
 "post_id"=>"google-search-and-search-engine-spam"}

私を助けてください。ありがとう。

4

1 に答える 1

1

#id メソッドを nil オブジェクトに呼び出そうとしています。この #id を呼び出す唯一の部分は、次を呼び出すときです。

@comment = Comment.build_from(@post, @user_who_commented.id, "コメント!" )

@user_who_commented はあなたの current_user です。current_user がありますか?

于 2011-02-04T08:13:16.473 に答える