undefined method `model_name' for NilClass:Class and unable to find a linked object
I am using rails v-3.0.3 and ruby gem 1.8 and I am working on creating a new class called contact_info and link it to the person class. Here are the code excerpts from all the relevant files:
model/person.rb
class Person < ActiveRecord::Base
has_one :contact_info, :dependent => :destroy
attr_accessible :first_name, :last_name, :middle_initial, :email, :password,
end
model/contact_info.rb
class ContactInfo < ActiveRecord::Base
belongs_to :person
attr_accessible :street_address_1, :street_address_2
end
controller/profile_chaperone_controller.rb
class ProfileChaperoneController < ApplicationController
def add_info
@person = current_person
end
views/profile_chaperone/add_info.html.erb
<% form_for @person.contact_info do |f| %>
<%= f.submit %>
<% end %>
and here are the error messages when I go to the add_info page:
undefined method `model_name' for NilClass:Class
Extracted source (around line #44):
41: <!--Add contact info for Snapshot section of Profile--!>
42: <div id="info">
43: <br>
44: <% form_for @person.contact_info do |f| %>
45:
46: <%= f.submit %>
47: <% end %>
I have also tried to create a form for @person and it does work. So the problem seems to be establish the proper开发者_如何学Go linkage between person and contact_info. Here is the migration file:
class CreateContactInfos < ActiveRecord::Migration
def self.up
create_table :contact_infos do |t|
t.integer :person_id
t.timestamps
end
end
def self.down
drop_table :contact_infos
end
end
I followed essentially the steps in agile programming in rails version 4, so I am really not sure how to fix this problem. Helps are deeply appreciated!
精彩评论