未定义方法`email' for nil:NilClass in Exibe 表Father 的邮件 [英] undefined method `email' for nil:NilClass in Exibe the mail of table Father

查看:29
本文介绍了未定义方法`email' for nil:NilClass in Exibe 表Father 的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我把这个归因于我评论模型:

class 注释 

这在用户模型中

class User 

但这不起作用:

 <% post.comments.each do |comment|%><div id="评论" ><%= comment.user.email %><%=comment.comment %>

<%end%>

出现错误:

 nil:NilClass 的未定义方法`email'

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

 @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

无效

同样的错误:

 nil:NilClass 的未定义方法`email'提取的源代码(围绕第 48 行):45:46: <% post.comments.each do |comment|%>47:<div id="评论">48:<%=comment.user.email %>49:<%=comment.comment%>50:</div>51:<%end%>

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

 attr_accessible :comment,:user_id,:post_id

我的表格是这样的

 

"comment" do |com|%><%= com.text_area :comment %><%= com.submit "aaa" %><%结束%>

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

解决方案

# Model班级评论ActiveRecord::Baseattr_accessible :comment, :user_id结尾#控制器@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

I have a problem, i make this atribbuition i comment model:

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

and this in user model

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

but this dont works:

  <% post.comments.each do |comment|   %>
    <div id="comments" >
      <%= comment.user.email %>
           <%= comment.comment %>
    </div>
   <%end%>

appear the error:

undefined method `email' for nil:NilClass

please what is the problem, in the create of the comment i make the atribbuition so , look:

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

how i solve this error, please-

UPDATE NEXT RESPONSES, THE ERROR PERSISTES:

I try this:

@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))

and this:

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

dont works

same error:

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%>

i dont know what is wrong my model comment have :user_id

  attr_accessible :comment,:user_id,:post_id

and my form make is this

   <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 %>

please help me i dont know where is the error, the db is migrate correctly

解决方案

# Model
class Comment < ActiveRecord::Base
  attr_accessible :comment, :user_id
end

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

But next would be better (:user_id is not accessible for mass-assignment):

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

这篇关于未定义方法`email' for nil:NilClass in Exibe 表Father 的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆