未定义的方法`email’代表nil:Exibe中的NilClass父表的邮件

我有一个问题,我做这个atribbuition我评论模型:

class Comment < ActiveRecord::Base attr_accessible :comment belongs_to :post belongs_to :user 

这在用户模型中

 class User < ActiveRecord::Base attr_accessible :email, :password, :password_confirmation has_many :posts has_many :comments 

但这不起作用:

   

出现错误:

 undefined method `email' for nil:NilClass 

请问有什么问题,在评论的创建中我做了atribbuition,看看:

  @comment = @post.comments.create(params[:comment],:user_id => current_user.id) 

我如何解决这个错误,请 –

更新下一个响应,错误持续:

我试试这个:

 @comment = Comment.new(params[:comment]) @comment.user = current_user @comment.post = @post @comment.save 

这个

 @comment = @post.comments.create(params[:comment].merge(:user_id => current_user.id)) 

还有这个:

 @comment = @post.comments.build(params[:comment]) @comment.user = current_user @comment.save 

不行

同样的错误:

 undefined method `email' for nil:NilClass Extracted source (around line #48): 45: 46:  47: 
48: 49: 50:
51:

我不知道我的模型评论有什么问题:user_id

  attr_accessible :comment,:user_id,:post_id 

我的表格就是这个

  <div id="comment_form_" style="display: none;" >  true,:class=>"comment" do |com| %>    

请帮助我,我不知道错误在哪里,数据库正确迁移

 # Model class Comment < ActiveRecord::Base attr_accessible :comment, :user_id end #Controller @comment = @post.comments.create(params[:comment].merge(:user_id => current_user.id)) 

但接下来会更好(:user_id无法进行质量分配):

 @comment = @post.comments.build(params[:comment]) @comment.user = current_user @comment.save 

如果您查看日志,您可能会看到有关尝试分配user_id的警告。 如果要使用attr_accessible,则需要添加要分配的所有属性。 更改

  attr_accessible :comment 

  attr_accessible :comment,:user_id 

怎么样

 @comment = Comment.new(params[:comment]) @comment.user = current_user @comment.post = @post @comment.save