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'sshow
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.