2011-02-03 80 views
1

我正在使用acts_as_commentable_with_threading gem。Rails 3 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"} 

请帮助我。谢谢。

回答

1

您尝试将#id方法调用为无对象。在那里你调用这个#ID只有部分是,当你拨打:

@comment = Comment.build_from(@post,@ user_who_commented.id, “注释!”)

和@user_who_commented是你的CURRENT_USER。你确定有current_user吗?

+0

谢谢,我今天试一试,并在这里写下结果。 – 2011-02-04 11:41:44