Note that there are some explanatory texts on larger screens.

plurals
  1. POundefined method `email' for nil:NilClass in Model Associations Rails, dont works
    primarykey
    data
    text
    <p>I have a problem, i make this atribbuition i comment model:</p> <pre><code>class Comment &lt; ActiveRecord::Base attr_accessible :comment belongs_to :post belongs_to :user </code></pre> <p>and this in user model</p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible :email, :password, :password_confirmation has_many :posts has_many :comments </code></pre> <p>but this dont works:</p> <pre><code> &lt;% post.comments.each do |comment| %&gt; &lt;div id="comments" &gt; &lt;%= comment.user.email %&gt; &lt;%= comment.comment %&gt; &lt;/div&gt; &lt;%end%&gt; </code></pre> <p>appear the error:</p> <pre><code>undefined method `email' for nil:NilClass </code></pre> <p>please what is the problem, in the create of the comment i make the atribbuition so , look:</p> <pre><code> @comment = @post.comments.create(params[:comment],:user_id =&gt; current_user.id) </code></pre> <p>how i solve this error, please-</p> <p>I try this:</p> <pre><code>@comment = Comment.new(params[:comment]) @comment.user = current_user @comment.post = @post @comment.save this @comment = @post.comments.create(params[:comment].merge(:user_id =&gt; current_user.id)) </code></pre> <p>and this:</p> <pre><code>@comment = @post.comments.build(params[:comment]) @comment.user = current_user @comment.save </code></pre> <p>dont works</p> <p>same error:</p> <pre><code>undefined method `email' for nil:NilClass Extracted source (around line #48): 45: 46: &lt;% post.comments.each do |comment| %&gt; 47: &lt;div id="comments" &gt; 48: &lt;%= comment.user.email %&gt; 49: &lt;%= comment.comment %&gt; 50: &lt;/div&gt; 51: &lt;%end%&gt; </code></pre> <p>i dont know what is wrong my model comment have :user_id</p> <p>attr_accessible :comment,:user_id,:post_id</p> <p>and my form make is this</p> <pre><code> &lt;div id="comment_form_&lt;%= post.id %&gt;" style="display: none;" &gt; &lt;%= form_for [post,post.comments.build], :remote =&gt; true,:class=&gt;"comment" do |com| %&gt; &lt;%= com.text_area :comment %&gt; &lt;%= com.submit "aaa" %&gt; &lt;%end %&gt; </code></pre> <p>please help me i dont know where is the error, the db is migrate correctly</p> <p>UPDATE:</p> <p>Have a before_filter and need login to see the page to comment, so have current_user, and i reset the bd and make new comments logged, i test meet getting the comment.user_id and get the id of the user but the method comment.user.email dont works, i will post the , the comment model is:</p> <pre><code> attr_accessible :comment,:user_id,:post_id belongs_to :post belongs_to :user validates_presence_of :comment ,:on =&gt;:create </code></pre> <p>the model user is:</p> <pre><code> class User &lt; 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 =&gt; :create validates_presence_of :email validates_uniqueness_of :email </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload