Hi, I'm a newbie with rad rails. I wanted to write at same time in two table from one form.
I have a table machine (with nom and role as column) and a table ipvfour (with machine_id and ip as column).
So I created in models the relation has-and-belongs-to-many.
But when I'm trying to add a new machine if failed with
unknown attribute: ip
I don't really understand why, can someone help me please ?
machine.controllers:
def create @machine = Machine.new(params[:machine])
ipvfour = @machine.ip.create(params[:ip])
respond_to do |format|
if @machine.save && ipvfour.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
new.html.erb (machine)
New machine
<% form_for(@machine) do |f_machine| %>
<%= render :partial => 'form', :locals => { :f_machine => f_machine } %>
<%= f_machine.submit 'Create' %>
<%= link_to 'Back', machines_path %>
_form.html.erb (machine)
<%= f_machine.error_messages %>
<% f_machine.fields_for :ip do |f_ip| %>
<%= render :partial => 'ipvfours/form', :locals => { :f_ip => f_ip } %>
<% end %>
_form.html.erb (ipvfours)
<%= f_ip.label :ip %><br />
<%= f_ip.text_field :ip %>
The page to add a machine is correclty displayed with all fields but it seems that write in db failed due to .... I hope that someone will be able ti help me.
Thanks in advance.