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 8712153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:03:12+00:00 2026-06-13T05:03:12+00:00

I’m getting the following error: undefined method `email’ for nil:NilClass Extracted source (around line

  • 0

I’m getting the following error:

undefined method `email' for nil:NilClass
Extracted source (around line #6):

3:   <div class="post-author">
4:     <div class="post-author-info">
5:       <span class="post-author-name">
6:         <strong><%= link_to comment.user.email, comment.user %></strong>
7:       </span>
8: 
9:       <span class="post-date secondary">

comments.rb:

  def show
    @comment = Comment.find(params[:id])
  end

  def create
    @post = Post.find(params[:post_id])
    comment_attr = params[:comment].merge :user_id => current_user.id
    @comment = @post.comments.create(comment_attr)
    redirect_to post_path(@post)
  end

post.rb:

  attr_accessible :title, :content

  belongs_to :user

  has_many :comments, :dependent => :destroy

user.rb:

 has_many :posts, :dependent => :destroy
  has_many :comments, :dependent => :destroy

comment.rb:

  attr_accessible :content, :user_id

  belongs_to :post, :counter_cache => true
  belongs_to :user

routes.rb:

  resources :posts do
    resources :comments
  end

schema.rb:

 create_table "comments", :force => true do |t|
    t.text     "content"
    t.integer  "post_id"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

create_table "users", :force => true do |t|
    t.string   "email",                  :default => "", :null => false
    t.string   "encrypted_password",     :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                             :null => false
    t.datetime "updated_at",                             :null => false
    t.string   "provider"
    t.string   "uid"
  end

I’m not sure what’s the problem. Everything seems fine in the files above.

Any suggestions?

EDIT:

posts/show.html.erb:

<div id="post-<%= @post.id %>" class="post-single">
  <h3 class="post-title"><%= @post.title %></h3>

  <span class="post-author-name">
    <strong><%= link_to @post.user.email, @post.user %></strong>
  </span>

  <span class="post-content">
    <%= @post.content %>
  </span>

  <div class="comments">
    <h4 class="comments-count"><%= pluralize(@post.comments_count, "Comment") %></h4>

    <%= render @comments %>
    <%= render 'comments/form' %>
  </div>
</div>

comments/_comment.html.erb:

<div id="comment-<%= comment.id %>" class="comment">

  <div class="post-author">
    <div class="post-author-info">
      <span class="post-author-name">
        <strong><%= link_to comment.user.email, comment.user %></strong>
      </span>

      <span class="post-date secondary">
        <%= time_ago_in_words(comment.created_at) %>
      </span>
    </div>

    <span class="post-author-control">
      <% unless current_user == nil %>
        <% if current_user.id == comment.user_id %>
          <%= link_to 'Edit', edit_post_comment_path(@post, comment) %>
        <% end %>
      <% end %>
    </span>
  </div>

  <div class="comment-body">
    <p><%= comment.content %></p>
  </div>
</div>

posts.rb:

  def show

    @post = Post.find(params[:id])
    @comments = @post.comments
    @comment = @post.comments.build
  end

comments/_form.html.erb:

Add a comment:

<%= form_for([@post, @comment]) do |f| %>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
  • 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-13T05:03:13+00:00Added an answer on June 13, 2026 at 5:03 am

    The problem is that the following line in your controller creates a comment object that is related to a post, but not to user and thus causes the error in your partial:

    @comment = @post.comments.build
    

    To get rid of the error you should either change the line above to:

    @comment = Comment.new
    

    or remove that line from your controller and change your comment form to:

    <%= form_for [@post, Comment.new] do |f| %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.