Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 模型关联Rails中nil:NilClass的未定义方法“email”无效_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 3.1_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 模型关联Rails中nil:NilClass的未定义方法“email”无效

Ruby on rails 模型关联Rails中nil:NilClass的未定义方法“email”无效,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,Ruby On Rails 3.2,我有一个问题,我做了这个atribbuition I comment模型: class Comment < ActiveRecord::Base attr_accessible :comment belongs_to :post belongs_to :user 请问问题是什么,在创建评论时,我使用了atribbuition,请看: @comment = @post.comments.create(params[:comment],:user_id => curre

我有一个问题,我做了这个atribbuition I comment模型:

class Comment < ActiveRecord::Base
  attr_accessible :comment
  belongs_to :post
  belongs_to :user
请问问题是什么,在创建评论时,我使用了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
this

@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:       <% post.comments.each do |comment|   %>
47:         <div id="comments" >
48:           <%= comment.user.email %>
49:                <%= comment.comment %>
50:         </div>
51:        <%end%>
模型用户是:

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



  attr_accessor :password
  before_save :encrypt_password

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_uniqueness_of :email

您必须确保,在保存注释时,当前_用户不是nil。 在您列出post.comments时,没有用户的评论是没有的


这就是你所需要的。如果无法再次使用,请向我们展示操作和模型的完整源代码。

您必须确保,当前用户在保存注释时不是零。 在您列出post.comments时,没有用户的评论是没有的


这就是你所需要的。如果它不再工作-向我们显示操作和模型的完整源代码。

请检查数据库注释表,它应该存储用户id。如果@post.comments的任何记录中没有用户id,则它会抛出错误。可以是用户id列中的一个或多个记录cantain nil


在用户id为空的注释中使用select*from

请检查数据库注释表,该表应存储用户id。如果@post.comments的任何记录中没有用户id,则会抛出错误。可以是用户id列中的一个或多个记录cantain nil


在用户id为空的注释中使用select*from

可能使用@post而不是查看文件中的post会有帮助吗?

可能使用@post而不是查看文件中的post会有帮助吗?

不要发布两次相同的问题:不要发布两次相同的问题:我已经更新了帖子,请查看此信息。谢谢我已经更新了帖子,请查看此信息。谢谢
@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:       <% post.comments.each do |comment|   %>
47:         <div id="comments" >
48:           <%= comment.user.email %>
49:                <%= comment.comment %>
50:         </div>
51:        <%end%>
   <div id="comment_form_<%= post.id %>" style="display: none;" >

      <%= form_for [post,post.comments.build], :remote => true,:class=>"comment" do |com| %>
          <%= com.text_area :comment %>
          <%= com.submit "aaa" %>

      <%end %>
 attr_accessible :comment,:user_id,:post_id
  belongs_to :post
  belongs_to :user
  validates_presence_of :comment ,:on =>:create
 class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation,:username
  has_many :posts
  has_many :comments



  attr_accessor :password
  before_save :encrypt_password

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_uniqueness_of :email