我正在使用rails v-3.0.3和ruby gem 1.8,我正在创建一个名为contact_info的新类,并将其链接到person类。以下是所有相关文件的代码摘录:
模型/ person.rb
class Person < ActiveRecord::Base
has_one :contact_info, :dependent => :destroy
attr_accessible :first_name, :last_name, :middle_initial, :email, :password,
end
模型/ contact_info.rb
class ContactInfo < ActiveRecord::Base
belongs_to :person
attr_accessible :street_address_1, :street_address_2
end
控制器/ profile_chaperone_controller.rb
class ProfileChaperoneController < ApplicationController
def add_info
@person = current_person
end
视图/ profile_chaperone / add_info.html.erb
<% form_for @person.contact_info do |f| %>
<%= f.submit %>
<% end %>
以下是我转到add_info页面时的错误消息:
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 %>
我也尝试为@person创建一个表单,它确实有效。所以问题似乎是建立人与contact_info之间的正确联系。这是迁移文件:
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
我基本上遵循了rails 4中的敏捷编程步骤,所以我真的不确定如何解决这个问题。非常感谢帮助!