Rails 3: nested_form, collection_select, accepts_nested_attributes_for and fields_for

后端 未结 1 879
不知归路
不知归路 2021-02-09 17:21

Update: answered here.

There are lots of good questions and answers here and on the interweb about getting nested_form, collection_select, accepts_nested_attributes_for

相关标签:
1条回答
  • 2021-02-09 18:04

    Huzzah! Here's the code which makes all this work. Bit verbose but didn't want to leave anything out. My main learnings:

    • you need to make the child attributes attr_accessible in the parent model

    • you need to make the parent and child ids attr_accessible in the join table model

    • it makes life easier if you build at least one child instance in the parent controller.

    contributor.rb model

    class Contributor < ActiveRecord::Base
      attr_accessible  #nothing relevant 
      has_many :contributors_isbns
      has_many :isbns, :through => :contributors_isbns
    

    isbn.rb model

    class Isbn < ActiveRecord::Base
      attr_accessible :contributors_attributes, :contributor_id, :istc_id #etc
      belongs_to  :istc
      has_many   :contributors, :through => :contributors_isbns
      has_many   :contributors_isbns
      accepts_nested_attributes_for :contributors #if you omit this you get a missing block error
    

    contributors_isbn model

    class ContributorsIsbn < ActiveRecord::Base
      belongs_to :isbn
      belongs_to :contributor
      attr_accessible :isbn_id, :contributor_id
    

    isbn controller

     def new
        @isbn  = Isbn.new
        @title = "Create new ISBN"
        1.times {@isbn.contributors.build}
      end
    

    new.html.erb

    <td class="main">
    <%= nested_form_for @isbn, :validate => false do |f| %>
    <h1>Create new ISBN</h1>
    <%= render 'shared/error_messages', :object => f.object %>
    <%= render 'fields', :f => f %>
      <div class="actions">
        <%= f.submit "Create" %>
      </div>  
    
    <% end %>
    

    _fields.html.erb

    <%= field_set_tag 'Identifier Details' do %>
    
    <li>
    <%= f.label 'Title prefix' %>
    <%= f.text_field :descriptivedetail_titledetail_titleelement_titleprefix %>
    </li>
    <li>
    <%= f.label 'Title without prefix' %>
    <%= f.text_field :descriptivedetail_titledetail_titleelement_titlewithoutprefix %>
    </li>
    <li>
    <%= f.label 'ISTC' %>
    <%= f.collection_select(:istc_id, Istc.all, :id, :title_title_text, :prompt => true) %>
    </li>
    
    <% end %>
    
    
    <%= field_set_tag 'Contributor' do %>
    <li>
    <%= f.label 'Contributor Sequence Number' %>
    <%= f.text_field :descriptivedetail_contributor_sequencenumber%>
    </li>
    
    <%= f.fields_for :contributors, :validate => false do |contributor_form| %>
    <li>
    <%= contributor_form.label :personnameinverted, 'Contributor Name' %>
    <%= contributor_form.collection_select(:isbn_id, Contributor.all, :id, :personnameinverted ) %>
    </li>
    <%= contributor_form.link_to_remove "[-] Remove this contributor"%>
    <% end %>
    <%= f.link_to_add "[+] Add a contributor", :contributors  %>
    
    
    <li>
    <%= f.label 'Contributor Role' %>
    <%= f.text_field :descriptivedetail_contributor_contributorrole  %>
    </li>
    
    <% end %>
    
    0 讨论(0)
提交回复
热议问题