Я пытаюсь записать в db идентификатор из другой таблицы. У меня есть таблица машин, которая принадлежит таблице владельцев.
Модели:
class Machine < ActiveRecord::Base
has_many :ipvfours, :dependent => :destroy
belongs_to :owners
accepts_nested_attributes_for :ipvfours
#accepts_nested_attributes_for :owners
# some check on fields
validates_uniqueness_of :nom, :scope => :nom
validates_length_of :nom, :within => 3..24
#validates_length_of :role, :within => 3..15
# return the value
def to_s
"#{nom},#{role}"
end
class Owner < ActiveRecord::Base
has_many :machines
#accepts_nested_attributes_for :machines
def name_owner
"#{name}"
end
end
в моей _form информация из таблицыотображается правильно, но у меня возникла ошибка при создании:
_form
<p>
<%= f_owner.label :owner %><br />
<%= f_owner.collection_select :owner_id, Owner.find(:all), :id, :name, :prompt => "Select an owner"%>
</p>
Мой контроллер создания:
def create
@machine = Machine.new(params[:machine])
respond_to do |format|
if @machine.save
flash[:notice] = 'Machine was successfully created.'
format.html { redirect_to(@machine) }
format.xml { render :xml => @machine, :status => :created, :location => @machine }
else
format.html { render :action => "new" }
format.xml { render :xml => @machine.errors, :status => :unprocessable_entity }
end
end
end
ОШИБКА, которая у меня есть:
NameError in MachinesController#create
uninitialized constant Machine::Owners
и запрос:
{"machine"=>{"nom"=>"fgj",
"owners"=>{"owner_id"=>"1"},
"role"=>"fgj",
"ipvfours_attributes"=>{"0"=>{"ip"=>"fgj"}}},
"commit"=>"Create",
"authenticity_token"=>"/j6/yo0KedbArk5Rj0SKGwIvg39+IMzmO78l/Fa7lHY="}
, хотя я думаю, что это должно быть что-то вроде:
{"machine"=>{"nom"=>"fgj",
"owner_id"=>{"owners"=>"1"},
"role"=>"fgj",
"ipvfours_attributes"=>{"0"=>{"ip"=>"fgj"}}},
"commit"=>"Create",
"authenticity_token"=>"/j6/yo0KedbArk5Rj0SKGwIvg39+IMzmO78l/Fa7lHY="}
, но я после многих попыток я надеваюне известно.Заранее спасибо.