我有2个模特:课堂和装备:
class Classroom < ActiveRecord::Base
attr_accessible :capacity, :close_at, :in_eritance, :lock_start, :lock_stop, :locked, :name, :note, :open_at
has_many :classrooms_outfits, dependent: :destroy
has_many :outfits, through: :classrooms_outfits
accepts_nested_attributes_for :outfits
end
class Outfit < ActiveRecord::Base
attr_accessible :name, :description
has_many :classrooms_outfits, dependent: :destroy
has_many :classrooms, through: :classrooms_outfits
end
***UPDATE***
#classrooms_outfits.rb
class ClassroomsOutfits < ActiveRecord::Base
belongs_to :classroom
belongs_to :outfit
end
这是一个schema.rb
#schema.rb
...
create_table "classrooms_outfits", :id => false, :force => true do |t|
t.integer "classroom_id"
t.integer "outfit_id"
end
add_index "classrooms_outfits", ["classroom_id", "outfit_id"], :name => "index_classrooms_outfits_on_classroom_id_and_outfit_id"
...
这是编辑教室的表格:
<%= form_for(@classroom) do |f| %>
<% f.fields_for :outfits do |outfit_form| %>
<%= outfit_form.text_field :outfit %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我遇到了这个错误:
NameError in Classrooms#edit
Showing
app/views/classrooms/_form.html.erb where line #50 raised:
uninitialized constant Classroom::ClassroomsOutfit
Extracted source (around line #50):
47: <%= f.label :note %><br />
48: <%= f.text_area :note %>
49:
50: <% f.fields_for :outfits do |outfit_form| %>
51: <%= outfit_form.text_field :outfit %>
52: <% end %>
53: <div class="actions">
Trace of template inclusion: app/views/classrooms/edit.html.erb
的更新: 的 在控制台我有这个输出:
1.9.3p484 :011 > Classroom::ClassroomsOutfit
NameError: uninitialized constant Classroom::ClassroomsOutfit
from (irb):11
from /home/cristiano/.rvm/gems/ruby-1.9.3-p484@booking/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /home/cristiano/.rvm/gems/ruby-1.9.3-p484@booking/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /home/cristiano/.rvm/gems/ruby-1.9.3-p484@booking/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.3p484 :012 > Classroom::ClassroomsOutfits
(irb):12: warning: toplevel constant ClassroomsOutfits referenced by Classroom::ClassroomsOutfits
=> ClassroomsOutfits(classroom_id: integer, outfit_id: integer)
1.9.3p484 :013 >
我认为“装备”的复数有问题,但我不知道解决它,你能帮助我吗?
答案 0 :(得分:0)
完成所有编辑/评论后。您需要在嵌套表单中实际渲染所需的字段..
<%= form_for(@classroom) do |f| %>
<% f.fields_for :outfits do |outfit_form| %>
<%= outfit_form.text_field :name %>
<%= outfit_form.text_field :description %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>