A contact has_many notes; notes belong_to a contact. In my notes controller, after a successful save of a Note, I redirect to the note's contact using:
format.html { redirect_to(@note.contact, :notice => 'Note was successfully created.') }
In my unit test, I'm testing the ability to create a note and redirect to the note's contact view page. My notes.yml fixture simply sets up the note, and in the setup portion of the notes_controller_test.rb I assign the note from the fixture to @note.
Here's the actual test code:
test "should create note" do
assert_difference('Note.count') do
post :create, :note => @note.attributes
end
end
I think the note is successfully saving, but the redirect is failing. So it looks like the redirect_to in the controller is throwing up the "Cannot redirect to nil!" error, but I can't seem to understand why.
Here is my Notes create action:
def create
@note = Note.new(params[:note])
respond_to do |format|
if @note.save
format.html { redirect_to(@note.contact, :notice => 'Note was successfully created.') }
format.xml { render :xml => @note.contact, :status => :created, :location => @note.contact }
else
format.html { render :action => "new" }
format.xml { render :xml => @note.contact.errors, :status => :unprocessable_entity }
end
end
end