我已将我的代码更新如下:
class CreateAddresses < ActiveRecord::Migration
def self.up
create_table :addresses do |t|
t.integer :address_type_id
t.text :street
t.string :city
t.string :state, :limit => 2
t.integer :zip
t.references :entry
t.timestamps
end
add_index :addresses, :entry_id
...
class CreateAddressTypes < ActiveRecord::Migration
def self.up
create_table :address_types do |t|
t.string :name
end
我的模型:
class AddressType < ActiveRecord::Base
has_many :addresses
attr_accessible :name
end
class Entry < ActiveRecord::Base
has_many :addresses
attr_accessible :email, :first_name, :last_name
end
class Address < ActiveRecord::Base
belongs_to :entry
belongs_to :address_type
attr_accessible :city, :state, :street, :zip
end
还有我的 _form 文件:
...
.form-inputs
= f.collection_select (:address_type_id, AddressType.all, :id, :name)
= f.input :street
= f.input :city
在我点击“创建”后,错误提示“无法批量分配受保护的属性:address_type_id”。我知道我必须在某个地方犯一些错误?