user938363
user938363

Reputation: 10358

1-to-many child record was not always saved with parent in rails 3.1.3

There are user and user_level in our rails 3.1.3 app. A user has many user_levels and a user_level belongs to a user.

The user_level record was saved successfully when the user_level object is created in 'new' like below:

  def new
    @user = User.new
    @user.user_levels.build     
  end

However the user_level record added with the following code (by railscasts.com #197) was not saved with the @user:

  def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.simple_fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
      render :partial => association.to_s, :locals => {:f => builder, :i_id => 0} 
    end
    link_to_function(name, "add_fields(this, \"#{association}\", \"#{j fields}\")")
  end

Both fields_for and simple_fields_for were tried without luck. The html code for inserted field was generated by f.simple_fields_for(association, new_object, :child_index => "new_#{association}"). But somehow the added field was not saved along with the @user.save.

In successful case, the user level object was created in 'new' with the @user.user_levels.build. In the unsuccessful case, the object was created with f.object.class.reflect_on_association(association).klass.new in 'link_to_add_fields'. We tried to use the obj.user_levels.build in 'link_to_add_fields' without luck.

Any thoughts about the problem? Thanks so much.

UPDATE:

here is the BAD string posted to the server when 2 positions selected:

{"name"=>"pur eng", "login"=>"tester10", "password"=>"password", "password_confirmation"=>"password", "user_type"=>"employee", "user_levels_attributes"=>{"1329335776431"=>{"user_levels"=>{"position"=>"mech_eng"}, "_destroy"=>"false"}, "1329335779261"=>{"user_levels"=>{"position"=>"elec_eng"}, "_destroy"=>"false"}}}

Here is the GOOD string posted to the server and saved the user levels successfully.

{"name"=>"tester eng", "login"=>"tester11", "password"=>"password", "password_confirmation"=>"password", "user_type"=>"employee", "user_levels_attributes"=>{"0"=>{"position"=>"elec_eng"}, "1"=>{"position"=>"inst_eng"}}}

Is the extra "user_levels" => in BAD string causing the problem? If it is, how to remove it?

Upvotes: 0

Views: 182

Answers (1)

Victor Rodrigues
Victor Rodrigues

Reputation: 149

As Ryan describes at the end of show notes:

Update: The code below has been changed slightly to work with Rails 3.x (the h escape call is removed)

def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => builder)
    end
    link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
end

Upvotes: 1

Related Questions