Rails form_for NameError in fields_for nested attribute

212 Views Asked by At

I have 2 models: Classroom and Outfit:

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

this is a piece of 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"
...

This is the form to edit a classroom:

<%= 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 %>

I had this error:

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

UPDATE: in console i have this output:

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 > 

I think there is a problem with the plural of "outfit" but I have no idea to solve it, can you help me?

1

There are 1 best solutions below

0
On

after all the edits/comments. You need to actually render the fields you want in the nested form..

<%= 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 %>