tags:

views:

17

answers:

1

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.

A: 

Your belongs_to association is referring to owners in the plural when it should be owner singular. A machine can only belong to one owner.

belongs_to :owner
Shadwell
ok thanks. But I still have one error after: ActiveRecord::AssociationTypeMismatch in MachinesController#createOwner(#59109312) expected, got HashWithIndifferentAccess(#40352280) and request now seems better. So I think the problem is amlost fixed :)
Goueg83460
The owner_id parameter in your request should be "owner_id"=>"1". If it is as you show in your answer {"owners=>"1"} then this is probably causing the error. When hash-like parameters are passed in rails converts them to a HashWithIndifferentAccess instance.
Shadwell
so the request should be : {"machine"=>{"nom"=>"fgj","owners"=>{"owner_id"=>"1"},"role"=>"fgj","ipvfours_attributes"=>{"0"=>{"ip"=>"fgj"}}}, "commit"=>"Create","authenticity_token"=>"/j6/yo0KedbArk5Rj0SKGwIvg39+IMzmO78l/Fa7lHY="}isn't it ? So even if I have a such request I still have same problem. Sorry if I don't understand correctly I'm a beginner with ruby on rails.
Goueg83460
No, it should be (like) {"machine"=> { "nom"=>"fgj", "owner_id"=>"1", "role"=>"fgj", ...... }
Shadwell
Have you got a nested for just for the owner? (Just spotted the f_owner). You don't need that; collection_select can be from the machine's form.
Shadwell
ok thanks i'ts working now :)
Goueg83460