nnyby
nnyby

Reputation: 4668

Accessing a model instance from a different model's controller

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:

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


In conclusion, which path should I pursue?


Update

To clarify,


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.

Upvotes: 0

Views: 1819

Answers (1)

zetetic
zetetic

Reputation: 47548

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.

Upvotes: 2

Related Questions