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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:20:45+00:00 2026-06-05T18:20:45+00:00

My routes.rb resources :post do resources :answers do resources :anscomments end end models post.rb

  • 0

My routes.rb

resources :post do
      resources :answers do
        resources :anscomments
      end
    end

models

post.rb

has_many :answers

answer.rb

belongs_to :post
has_many :anscomments

anscomment.rb

belongs_to :answer

controller

answers_controller.rb

class AnswersController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @answer = @post.answers.create(params[:answer])
    redirect_to post_path(@post)
  end
end

anscomments_controller.rb

class AnscommentsController < ApplicationController
  def create
    @answer = Answer.find(params[:answer_id])
    @anscomment = @answer.anscomments.create(params[:anscomment])
    redirect_to post_path(@answer)
  end
end

views

view/post/show.html.erb

<table align=center width="60%" bordercolor="black">

    <tr>
        <td align="center">
            <h2>
                <%=@post.title%> 
            </h2>
        </td>
    </tr>
    <tr>
        <td align="center">
            <h3><%=@post.body%></h3>
        </td>
    </tr>
    <tr>
        <td align="center">
            This Post comes under:<%=@post.tag%>
        </td>
    </tr>

</table>
<br />
<h2 align="center">Answers</h2>
<div align="center">
    <% @post.answers.each do |answer| %>
    <table width="50%" border="1">
        <tr>
            <td align="center">
                <%= answer.body %>
            </td>
        </tr>



        <div align="center">
            <% @answer.anscomments.each do |anscomment| %>
            <table width="40%" %>
                <tr>
                    <td>
                        <%= anscomment.body %>
                    </td>
                </tr>
            </table>
            <%end%>
        </div>
        <div align="center">
            <%= form_for([@answer,@answer.anscomments.build]) do |f| %>
            <table>
                <tr>
                    <td><%= f.text_area :body %></td>
                </tr>
                <tr>
                    <td><%= f.submit "Submit Comment" %></td>
                </tr>
            </table>
            <%end%>
        </div>



    </table>
    <% end %>
</div>
<br />
<h2 align="center">Your Answer</h2>
<div align="center">
    <%= form_for([@post, @post.answers.build]) do |f| %>
    <table>
        <tr>
            <td><%= f.text_area :body %></td>
        </tr>
        <tr>
            <td><%= f.submit "Post Your Answer" %></td>
        </tr>
    </table>
    <% end %>
</div>

when i tried to click on individual post i am getting an error like—-“undefined method `anscomments’ for nil:NilClass”

please find a solution…..

  • 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-05T18:20:46+00:00Added an answer on June 5, 2026 at 6:20 pm

    Try to use answer.anything instead of @answer.anything in post/show.html.erb

    <%= answer.body %>
    

    is correct,

    <% @answer.anscomments.each do |anscomment| %>
    

    error, @answer is not initialized (nil)

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

Sidebar

Related Questions

routes.rb: resources :user do post 'profile', :on => :member end I'm trying to link
trail has_many chapters chapter belongs_to trail my routes: resources :trails do member do resources
In my routes I have: resources :accounts This produces: accounts GET /accounts(.:format) accounts#index POST
These are my routes: resources :auctions do resources :bids end I have the auctions#show
in routes.rb resources :restaurants do collection do get 'nearby' end resources :tickets end resources
I have a route: resources.router.routes.page.route = page/:page resources.router.routes.page.defaults.module = core resources.router.routes.page.defaults.controller = page resources.router.routes.page.defaults.action
In routes.rb i have: resources :themes do resources :messages end In messages_controller_test.rb i have:
in my routes.rb I have: resources :boards do resources :items end now in boards#show
I'm using the will paginate gem from http://github.com/mislav/will_paginate Routes: map.resources :post do |post| post.resources
I have the following in my routes file: resources :timelogs do member do post

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.