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
@commentin the Thread’sshowcontroller 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
createaction, 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.
Are you sure that
Thread.newis creating an instance of your model? Ruby has a thread library that also defines aThreadclass — you may have a name collision.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 thatbuilddoes 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:
Then in your comments controller you’ll get
spool_idin the params hash. The path will look like so for existing comments:and like so for new comments
Where
params[:spool_id]is set to 1.