views:

28

answers:

1

Hello, rails 3 newbie, using Devise for auth...

I want to create the following models:

class Instance < ActiveRecord::Base
 has_many :users
 has_many :notes
end 

class User < ActiveRecord::Base
 belongs_to :instance
end

class Note < ActiveRecord::Base
 belongs_to :instance
end

To create a new note in the notes_controller.rb

def create
 @note = instance.notes.build(params[:note].merge(:instance_id => current_user.instance_id))
end

But I'm getting the following ERROR: "undefined local variable or method `instance' for #"

Ideas?

A: 

You haven't assigned anything to "instance" yet, so there's nothing to reference. If you know the instance record already exists in the database, you could do something like:

@instance = current_user.instance
@note = Note.create(:instace_id => @instance.id)

If not, you'd need to check and create it first if necessary, using the same kind of syntax.

bnaul
And this belong in the controller for the note, is that right?
AnApprentice
@nobosh yep. That code gets called when you hit the corresponding link, e.g. /notes/create, and then whatever instance variables you declare in that controller method are also accessible in the view.
bnaul
Thanks bnaul, really appreciate it!
AnApprentice