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

  • Home
  • SEARCH
  • 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 6233737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:21:12+00:00 2026-05-24T10:21:12+00:00

I’m trying to create a way to have users comment on my posts. Currently

  • 0

I’m trying to create a way to have users comment on my posts. Currently I have All user posts showing up on my home page and then in the user profile only the current users posts. I would like to have it so that the comments appear only on the posts in the users profile. I tried to add a comment form in the user profile but I got an undefined method `comments’ for nil:NilClass error.

My comments_controller looks like

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post])
    @comment = @post.comments.create(params[:comment])
    redirect_to post_path(@post)
end

I have a partial (_comment_form.html.erb) that I am rendering in the user profile which looks like

<h2>Add a comment:</h2>
<%= form_for ([@post, @post.comments.build]) do |f| %>
   <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
   </div>
   <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
   </div>
   <div class="actions">
    <%= f.submit %>
   </div>
<% end %>

My Comment model looks like

class Comment < ActiveRecord::Base
  belongs_to :post
end

My Post model looks like

class Post < ActiveRecord::Base
 attr_accessible :content

 belongs_to :user

 validates :content, :presence => true
 validates :user_id, :presence => true
 validates :user, :presence => true
 validates :title, :presence => true

 has_many :comments

 default_scope :order => 'posts.created_at DESC'
end

My User Profile Looks like show.html.erb

<table class="profile" summary="Profile information">
  <tr>
    <td class="main">
    <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
    </h1>
    <% unless @user.posts.empty? %>
        <table class="posts" summary="User posts">
            <%= render @posts %>
            <%= render 'comments/comment_form' %>
        </table>    
    <% end %>
    </td>
    <td class="sidebar round">
  <strong>Name</strong> <%= @user.name %><br />
  <strong>URL</strong>  <%= link_to user_path(@user), @user %><br />
  <strong>Tasks</strong> <%= @user.posts.count %>
    </td>
  </tr>
</table>
  • 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-05-24T10:21:13+00:00Added an answer on May 24, 2026 at 10:21 am

    It might be that you haven’t initialized @post in the new method of your controller and it’s being used as nil. Always construct an empty model for your new form if it’s practical:

    def new
      @post = Post.new(params[:post])
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.