I'm trying to write in db an id from another tableI have a machine table that belongs to owner table.
Models:
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
in my _form information from table are correctly displayed but I have an error during create:
_form
<p>
<%= f_owner.label :owner %><br />
<%= f_owner.collection_select :owner_id, Owner.find(:all), :id, :name, :prompt => "Select an owner"%>
</p>
My create controler:
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
ERROR that I have:
NameError in MachinesController#create
uninitialized constant Machine::Owners
and request is:
{"machine"=>{"nom"=>"fgj",
"owners"=>{"owner_id"=>"1"},
"role"=>"fgj",
"ipvfours_attributes"=>{"0"=>{"ip"=>"fgj"}}},
"commit"=>"Create",
"authenticity_token"=>"/j6/yo0KedbArk5Rj0SKGwIvg39+IMzmO78l/Fa7lHY="}
while I think it should be something like:
{"machine"=>{"nom"=>"fgj",
"owner_id"=>{"owners"=>"1"},
"role"=>"fgj",
"ipvfours_attributes"=>{"0"=>{"ip"=>"fgj"}}},
"commit"=>"Create",
"authenticity_token"=>"/j6/yo0KedbArk5Rj0SKGwIvg39+IMzmO78l/Fa7lHY="}
but I after a lot's of try I don't known. Thanks in advance.