Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:43:05+00:00 2026-06-17T12:43:05+00:00

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

  • 0

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-

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

UPDATE:

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:

 attr_accessible :comment,:user_id,:post_id
  belongs_to :post
  belongs_to :user
  validates_presence_of :comment ,:on =>:create

the model user is:

 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:43:06+00:00Added an answer on June 17, 2026 at 12:43 pm

    May be using @post instead of post in view file will help?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, i make this atribbuition in model comment.rb has_one :user And my form is
I have a huge problem with this method im trying to make. I have
I make the problem shorter. Actually I have data much longer than this. I
I have problem with base classes in WPF. I try to make a base
I am doing my research. I have problem to make code to present this
I have make this script: $(#comments .comment .links).hide(); $(#comments .comment).hover( function() { $(.links, this).stop(true).slideDown(300);
I have problem to make work a resource file. This is my simple code
I have the following problem: I make the call to the method of a
I have to make a simple layout in android but have problem with the
I have following problem: I have to make a ASP.NET Webapplication with a two-row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.