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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:02:22+00:00 2026-05-16T03:02:22+00:00

I have a Thread model which has a show page, and in that action’s

  • 0

I have a Thread model which has a show page, and in that action’s view code I have:

<%= render 'comments/form' %>

What is the proper way to initialize the new Comment? I’ve tried:

  • @comment = @thread.comments.build(params[:comment]) in the Comment create action and the comment form’s view code.
  • Thread helper methods and Model methods called things like this_thread.
  • Initializing @comment in the Thread’s show controller code.

All of these attempts led to nil object errors or undefined method errors.


In conclusion, which path should I pursue?


Update

To clarify,

  • I create a new comment in the Thread controller. @comment = @thread.comments.build
  • In the comment create action, how do I access the @comment I created over in the thread controller?

Update #2

The Comment controller:

def create
  thread = Thread.find(params[:id])
  @comment = thread.comments.build(params[:thread])

  if @comment.save
  ...
end

is giving me this error:

Couldn't find Thread without an ID

Do you know why? I’m assuming it has something to do with params[:id] being the comment id and not the thread id. How, then, do I get the thread id? Or, what Rails magic am I forgetting to rely on, since this is a has_many/belongs_to relationship.

  • 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-16T03:02:22+00:00Added an answer on May 16, 2026 at 3:02 am

    Are you sure that Thread.new is creating an instance of your model? Ruby has a thread library that also defines a Thread class — you may have a name collision.

    In the comment create action, how do I
    access the @comment I created over in
    the thread controller?

    Technically, you don’t. When your view posts to the Comment controller, a new object has to be created, e.g. @comment = Comment.new(params[:comment]. Or if you are retrieving from the DB, @comment = Comment.find(params[:id]). Note that build does not save the object.

    EDIT

    To get the thread in the controller you have two options. One way is to pass it in as part of the posted form (typically using a hidden field).

    A better way is to use nested routes and let Rails do the work for you. If you have a route like:

    map.resources :spools do |spools|
      spools.resources :comments
    end
    

    Then in your comments controller you’ll get spool_id in the params hash. The path will look like so for existing comments:

    /spools/1/comments/2
    

    and like so for new comments

    /spools/1/comments/
    

    Where params[:spool_id] is set to 1.

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

Sidebar

Related Questions

I have a .NET Windows Service which spawns a thread that basically just acts
I have an object, RenderComponent , that has a property named Model that is
I have thread exception handler which saves the exception stack trace and should close
I have one thread that writes results into a Queue. In another thread (GUI),
I have some thread-related questions, assuming the following code. Please ignore the possible inefficiency
I have a thread that, when its function exits its loop (the exit is
I have a thread that needs to be executed every 10 seconds. This thread
I have a thread in Ruby. It runs a loop. When that loop reaches
I have a Windows service that has a number of threads that all need
I know that model should not be aware of session data but I have

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.