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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:37:06+00:00 2026-05-21T18:37:06+00:00

I created a message board for my application and uses the following three models

  • 0

I created a message board for my application and uses the following three models to work the whole thing: Forums have many topics, and topics have many posts. Posts is a nested resource of Topic, and whenever a user chooses “Create New Topic,” the ‘new’ action has a nested post to start the thread. Here is the relative code of that…

topics_controller.rb

class TopicsController < ApplicationController
  load_and_authorize_resource
  def new
    @topic = Topic.new
    @post = @topic.posts.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @topic }
    end
   end

   [...]

topic.rb

class Topic < ActiveRecord::Base
  has_many :posts
  belongs_to :user, :counter_cache => TRUE
  belongs_to :forum, :counter_cache => TRUE
  validates :title, :length => { :maximum => 95 }, :presence => { :message => "You need to title your topic." }

  accepts_nested_attributes_for :posts
end

post.rb

class Post < ActiveRecord::Base
  belongs_to :user, :counter_cache => TRUE
  belongs_to :forum, :touch => TRUE, :counter_cache => TRUE 
  belongs_to :topic, :touch => TRUE, :counter_cache => TRUE 

  validates :body, :presence => { :message => "You have not written any text in the body." }
end

new.html.erb

<%= form_for(@topic, :url => forum_topics_path) do |f| %>
   <%= f.label :title %><%= f.text_field :title %>

   <%= fields_for(@post) do |cf|%>
      <%= cf.label :body %><%= cf.text_area :body, :cols=> 108, :rows => 10 %>
   <% end %>

   <%= f.submit  %>
<% end %>

Anyway, I completed this and it worked fine.

Up until now. Just out of the blue for no reason I can spot whatsoever, whenever I select “Create a new topic” and directed to the ‘new’ action on Topics, I get a error of “NoMethodError in Topics#new: You have a nil object when you didn’t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.[]”

This makes absolutely no sense as I looked back at previous versions in git, and there have been no earth shattering changes made to this code. Perhaps some updated Rails version (3.0.7) or Gem is responsible, but I dunno about that.

Anyway, the line it’s throwing a fit on is in the html.erb file, <%= fields_for(@post) do |cf|%>. Uh…it’s new so it’s supposed to be nil, right? Take this out, and it works. I’ve tried redefining @post in the Topic controller as Post.new, but that returns the same error. In addition, anything I’ve googled on the subject says @post = @topic.posts.build is the way to go.

So am I doing something wrong in the code above? And any idea why it would work previously, but not now?

  • 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-21T18:37:07+00:00Added an answer on May 21, 2026 at 6:37 pm

    Try this, make sure it’s f.fields_for and not just fields_for.

    <%= form_for(@topic, :url => forum_topics_path) do |f| %>
       <%= f.label :title %><%= f.text_field :title %>
    
       <%= f.fields_for(@post) do |cf|%>
          <%= cf.label :body %><%= cf.text_area :body, :cols=> 108, :rows => 10 %>
       <% end %>
    
       <%= f.submit  %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple GtkStatusBar created with glade,and want to add a simple message
I have created a forum board app, based on YAML schema found in 'real
I have a message board in ASP.Net MVC and when the user creates a
I'm working on a web application. A user will create an email message that
I create new ASP.NET web application that use SMTP to send message. The problem
I had the following idea: Say we have a webapp written using django which
I've read about it on a message board - Random class isn't really random.
I am using cakephp 1.26 to create a simple message board in my localhost.
Fairly simple concept, making an extremely basic message board system and I want users
I'm using CakePHP 1.3, and trying to make a simple message posting board with

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.