看不出为什么我得到未定义的方法form_for

Ric*_*wis 0 ruby forms activerecord controller ruby-on-rails-3

我不明白为什么我收到这个错误

undefined method `sector_id' for #<Portfolio:0x007fe17c2e3848>
Run Code Online (Sandbox Code Playgroud)

我有一个投资组合模型和一个区域模型,它们看起来像这样

class Portfolio < ActiveRecord::Base
  belongs_to :sector
  attr_accessible :overview, :title, :sector_id
end



class Sector < ActiveRecord::Base
  has_many :portfolios
  attr_accessible :name
end
Run Code Online (Sandbox Code Playgroud)

我的路线

resources :portfolios do
  resources :sectors
end
Run Code Online (Sandbox Code Playgroud)

因此,在我的表单中创建一个新的投资组合,我有这个collection_select

<%= f.label :sector_id, "Choose Sector", :class => 'title_label' %><br>
<%= f.collection_select(:sector_id, Sector.all, :id, :name, :prompt => "Please Select a Sector") %>
Run Code Online (Sandbox Code Playgroud)

这是我以前做了很多次并且它已经工作了,有人能看出为什么我会收到这个错误吗?

我能想到的唯一一件事就是我把我的控制器称为投资组合,我总是混淆了复数和单一的控制器名称,这会对我的情况产生影响吗?

p.m*_*los 5

也许您还没有运行迁移,这会在表"投资组合"中添加"sector_id"列.如果您使用MySQL连接到您的数据库并检查表(show create table portfolios;).如果您正在使用其他rdbms,请使用适当的方法从数据库服务器获取此信息.或者,在rails console(rails c)中输入Portofolio并查看它打印出的属性.它包括sector_id吗?