NameError未初始化的常量

时间:2012-06-19 20:17:54

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 associations simple-form

尝试渲染表单时:

NameError in Deals#new

Showing /Users/nelsonkeating/Desktop/ReminDeal1/app/views/deals/_form.html.erb where line #8 raised:

uninitialized constant Deal::Interests
Extracted source (around line #8):

5:  <li><%= f.input :name %></li>
6:  <li><%= f.input :copy %></li>
7:  <li><%= f.association :city %></li>
8:   <li><%= f.association :interests, :label => false, :as => :check_boxes %></li>
9:  </ul>
10: 
11:   <div class="form-actions">

该应用程序的相关部分如下:

型号:

class Deal < ActiveRecord::Base
   attr_accessible :title, :body
   belongs_to :interests
   belongs_to :city
end

class Interest < ActiveRecord::Base
  attr_accessible :name
  has_many :person_interests
  has_many :deals
end

控制器:

deals_controller.rb
  def new
    @deal = Deal.new
end

查看:

<%= simple_form_for(@deal) do |f| %>
  <%= f.error_notification %>
<ul class="testss1">

    <li><%= f.input :name %></li>
    <li><%= f.input :copy %></li>
    <li><%= f.association :city %></li>
    <li><%= f.association :interests, :label => false, :as => :check_boxes %></li>
 </ul>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

有什么想法吗?非常感谢!

1 个答案:

答案 0 :(得分:2)

应为belongs_to :interest

如果您需要多个兴趣,可能需要has_many:through