Git Product home page Git Product logo

nested_form_deep_association_bug's Introduction

Bug description

Copy of my issue in nested_form repo (github.com/ryanb/nested_form/issues/267)

I experienced a bug with deep nested form. Saying short: when intermediate association (book in library–>book–>story) have no attributes on form except nested attributes - then nested_form generates wrong ids(all the same) so that the only story can be obtained in controller from params hash.

I have three models:

class Library < ActiveRecord::Base
  has_many :books
  accepts_nested_attributes_for :books
end
class Book < ActiveRecord::Base
  belongs_to :library
  has_many :stories
  accepts_nested_attributes_for :stories
end
class Story < ActiveRecord::Base
  belongs_to :book
end

And a libraries controller:

class LibrariesController < ApplicationController
  def new
    @library = Library.new
    @library.books.build
  end

  def create
    render text: params.inspect
  end
end

My form for library looks in such a way:

<%= nested_form_for(@library) do |f| %>
  <div class="field">
    <%= f.label :name_of_library %><br>
    <%= f.text_field :name_of_library %>

    <%= f.fields_for :books  do |book_f| %>
      <%= book_f.label :title %>
      <%= book_f.text_field :title %>

      <%= book_f.fields_for :stories do |story_f| %>
        <%= story_f.label :subtitle %>
        <%= story_f.text_field :subtitle %>

        <%= story_f.link_to_remove 'Remove story' %>
      <% end %>

      <%= book_f.link_to_add 'Add story', :stories %>
    <% end %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

When I create a library I automatically fill it with one book consisting of several stories (it looks weird, but in initial task there were portfolios and one photo collection full of photos associated to it which makes more sense).

I add two stories to a book. All works ok, and my params hash after sending a request (here I removed unnecessary fields) is:

{"library"=>{"name_of_library"=>"Babylon library", "books_attributes"=>{"0"=>{"title"=>"Book of books", "stories_attributes"=>{"1368882624930"=>{"subtitle"=>"True story", "_destroy"=>"false"}, "1368882631410"=>{"subtitle"=>"False story", "_destroy"=>"false"}}}}}}

Then I remove input for book name (photo collection in portfolio can be without name) from the form, fill the rest fields the same way and get the only story because now all inputs on the form have the same id, and latter stories rewrite older stories. Params hash is:

{"library"=>{"name_of_library"=>"Babylon library", "books_attributes"=>{"0"=>{"stories_attributes"=>{"1368882731158"=>{"subtitle"=>"False story", "_destroy"=>"false"}}}}}}

One can get an application at github.com/prijutme4ty/nested_form_deep_association_bug and try to run ‘/libraries/new` (this app is rails 4.0.rc1 but nested_form on rails 3 experience exactly the same problem) and try it with book title in form or without. You can just remove `=` signs not to render these fields:

<%= book_f.label :title %>
<%= book_f.text_field :title %>

I don’t understand what exactly is wrong with inputs on form, but they’re obviously unexpected. Also this bug exists only on unsaved associations, if I save intermediate books association before rendering form - all works (but creating objects in ‘#new` action is the very wrong way).

nested_form_deep_association_bug's People

Contributors

vorontsovie avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.