Instead of update, nested form creates a new one Rails

I have a form with 2 nested forms for a Professional (certificates and trainings). Each one has the association accepts_nested_attributes_for in the Professional model. I can create a new professional with his certificates and training. When I save, however, instead of updating the certificates and trainings a new one is created.

My Professional controller:

def edit @professional = Professional.find(params[:id]) @component = Component.all cert = Certificate.where('professional_id = ?', params[:id]) cert.each do |certificate| @certificate = certificate end tra = Training.where('professional_id = ?', params[:id]) tra.each do |training| @training = training end @work = Work.all @charge = Charge.all @entities =Array.new TypeEntity.where("id IN (1,5)").each do |tent| @entities << tent.entities end @reg = Time.now.to_i.to_s @major = Major.all @action = 'edit' render layout: false end def update profesional = Professional.find(params[:id]) if profesional.update_attributes(professional_parameters) flash[:notice] = "Se ha actualizado correctamente los datos." redirect_to :action => :index else profesional.errors.messages.each do |attribute, error| flash[:error] = flash[:error].to_s + error.to_s + " " end # Load new() @profesional = profesional render :edit, layout: false end end private def professional_parameters params.require(:professional).permit( :name, :dni, :professional_title_date, {:major_ids => []}, :date_of_tuition, :code_tuition, :professional_title, :tuition, :cv, certificates_attributes: [ :id, :professional_id, :work_id, :charge_id, :entity_id, :num_days, :start_date, :finish_date, {:component_work_ids => []}, :certificate, :other, :_destroy ], trainings_attributes: [ :id, :professional_id, :type_training, :name_training, :num_hours, :start_training, :finish_training, :training, :_destroy ] ) end

This are the models:

class Certificate < ActiveRecord::Base has_one :charge belongs_to :professional end class Professional < ActiveRecord::Base has_and_belongs_to_many :majors has_many :certificates has_many :trainings accepts_nested_attributes_for :certificates, :allow_destroy => true accepts_nested_attributes_for :trainings, :allow_destroy => true end class Training < ActiveRecord::Base belongs_to :professional end

The form begins with this:

=simple_form_for([:biddings, @professional], html: { multipart: true }) do |f|

after that I have the 3 forms in a bootstrap wizard.

-------------Problems Reply------------

Make sure you have ID attribute in Hash in your Edit form. As your Model has One-to-Many,

For each hash that does not have an id key a new record will be instantiated, unless
the hash also contains a _destroy key that evaluates to true.

Category:ruby on rails Views:0 Time:2014-04-29

Related post

  • Mongoid - Updating Nested Attributes 2012-02-06

    From the mongoid docs: Consider a member that has a number of posts: class Member include Mongoid::Document has_many :posts accepts_nested_attributes_for :posts end You can now set or update attributes on an associated post model through the attribut

  • mongodb-Mongoose update nested sets- why is this so hard arg 2012-08-29

    I'm trying to fix some data globally... Why am i getting this error...TypeError: Object function model(doc, fields, skipId) { if (!(this instanceof model)) r I am getting all the data and it should update, but its not. and the error is obtuse. Update

  • Who is the acting user for cron updates drupal when creating a triggered rule? 2010-07-29

    Who is the acting user for cron updates drupal when creating a triggered rule? What I'm referring to is the php variable available to the developer when using the custom php code option in the "add a condition" widget. Is the acting user supposed to

  • In mySQL, can you update a newly created table with fields from existing tables? 2011-03-10

    I am new to mySQL and would like some help with a query I'm running. I'd like to update a newly created table with fields from existing tables. Here is what I'm working with. UPDATE NEW_TABLE new, OLD_TABLE_1 one, OLD_TABLE_2 two, OLD_TABLE_3 three S

  • Entity Framework Code First - Update nested object directly 2011-04-22

    I use repository per class with UnitOfWork pattern in Winform application and I want update nested object by it's Id directly in an separated window, but it not work. My classes are like this: public class Studio { public int Id { get; set; } public

  • mongoid: update nested attributes 2011-06-10

    i am running into an issue with updates to nested attributes for referenced documents not being persisted > u = User.first => #<User... > u.changes => {} > u.profile.changes => {} > u.attributes = {:profile_attributes => {:

  • ExtJS 4 problems with form updating a new created record in a store - loadRecord updateRecord 2011-11-16

    I have problems with a form saving to a store. The form loads a record for editing a existing record, or is blank for a new record. Editing a existing record works fine. Creating a new one works fine as well. I get problems if I want to edit & up

  • Aptana Studio 3 tries to create new project with "rails ." instead of "rails new ." on update of rails 2011-11-22

    I am using Aptana Studio 3 and having trouble creating a rails project in the IDE. Aptana tries to create a new rails project using the command rails . instead of rails new . I used the solution at: Cannot create a new Rails Project with Aptana Studi

  • Updating a book created with iBooks Author 2012-02-20

    I have a quick question about Apple's iBooks Author: is it in any way possible to update a book created with iBooks Author? I mean: if I create a book, publish it and users download it, can I change it afterwards so that users get a notification and

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.126 (s). 11 q(s)