0

I tried to follow railscast 196 nested model , but I could not get it to work . The add_link_fields was not able to add any field to the form. I tried to use different javascript code. Non of them worked for me. Here is customer Form which has several orders.

<%= simple_form_for @customer , :html => {:class => 'form-horizontal' }  do |f| %>
<%= f.input :first_name, :hint => 'First Name' %>
<%= f.input :last_name, :hint => 'Last Name' %>
<%= f.input :email , :hint => 'Email' %>
<%= f.input :address,  :label => false, :hint => 'Address' %>
<%= f.input :customer_number, :label => false, :hint => 'Customer Number'  %>
<%= f.input :birthday,  :label => false, :hint => 'Birthday'  %>   
</div>
      <div class="tab-pane fade" id="profile">
       <div class="text-left">     
            <ul>
               <%= f.simple_fields_for :orders do |builder| %>    
              <li class = "col-lg-4">    
                  <p><%= render 'order_fields' , :f => builder %></p>    
                <li />  
                <% end %>
          <%= link_to_add_fields "Add Question", f, :orders %>
                <ul />   
            </div>
         </div>

Here is the partial ,_order_fields:

<%= f.input :order_number , :label => false , :hint => 'Number' %> 
<%= f.input :order_date, :as => :string , :label => false, :hint => 'Date' %> 
<%= f.input :sum  , :label => false , :placeholder => 'Sum', :hint => 'Sum'%>
<%= f.hidden_field :_destroy %>

here is my new action from customer controller :

 def new
 @customer = Customer.new
 @customer.orders.build    
 end

and set_params action is here :

 def customer_params
       params.require(:customer).permit(:id, :first_name, :last_name, :email, :address,          :birthday, :customer_number, :orders_attributes => [:id, :order_number, :order_date, :sum, :customer_id,:quantity_of_product,:quantity,:list_of_products])
    end

and the javascript :

 $('form').on 'click', '.add_fields', (event) ->
        time = new Date().getTime()
        regexp = new RegExp($(this).data('id'), 'g')
        $(this).before($(this).data('fields').replace(regexp, time))
        event.preventDefault()

and finally helper method :

def link_to_add_fields(name, f, association)
  new_object = f.object.send(association).klass.new
   id = new_object.object_id
   fields = f.fields_for(association, new_object, child_index: id) do |builder|
  render(association.to_s.singularize + "_fields", f: builder)
end

link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end

And this is my customer model :

class Customer < ActiveRecord::Base
    has_many :orders
    has_and_belongs_to_many :products
    accepts_nested_attributes_for :orders, :allow_destroy => true
end

I tried several other javascript as well, Non of them could't help me. What I am missing here? I edited this question.

Oscar Liam
  • 31
  • 4
  • By the way I got no error at all when I try to test it.And I also add here . the add_link_fields does not add any field . – Oscar Liam May 27 '14 at 07:10
  • you haven't told us what is not working. what's it? I see you your comment says `add_link_fields` does not add any fields. is that your problem? post your models – Wally Ali May 27 '14 at 07:20
  • I remember going through the same railscast episode. the `add_link_fields` didn't work for me too. Now I remember. I opted using for another gem that does exactly that with ease. it is called cocoon. if you want to use cocoon, I can help you with that. – Wally Ali May 27 '14 at 07:27
  • class Customer < ActiveRecord::Base has_many :orders has_and_belongs_to_many :products accepts_nested_attributes_for :orders, :allow_destroy => true EMAIL_REGEX = /\A[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}\Z/i validates :first_name, :presence => true, :length => { :maximum => 25 } validates :last_name, :presence => true, :length => { :maximum => 50 } validates :email, :presence => true, :length => { :maximum => 100 }, :format => EMAIL_REGEX end – Oscar Liam May 27 '14 at 07:27
  • add_link_fields does not work. – Oscar Liam May 27 '14 at 07:28
  • If it does not work I can use that gem , let me know. – Oscar Liam May 27 '14 at 07:30
  • I found the gem . I will give it try later. Thanks – Oscar Liam May 27 '14 at 07:33

3 Answers3

0

I went through Railscast 196 just like you and the link_to_add_fields helper method didn't work for me. Instead, I opted for cocoon which does exactly that with ease. Cocoon provides two helper methods:

link_to_add_association and link_to_remove_association

and it's got a complete example using simple_form, standard rails forms and formtastic

It is easier to follow the examples

Wally Ali
  • 2,500
  • 1
  • 13
  • 20
0

I got it to work by following YvonC's answer.

Pasting it here for ease:

$(document).on 'click', 'form .remove_fields', (event) ->
  $(this).prev('input[type=hidden]').val('1')
  $(this).closest('fieldset').hide()
  event.preventDefault()

$(document).on 'click', 'form .add_fields', (event) ->
  time = new Date().getTime()
  regexp = new RegExp($(this).data('id'), 'g')
  $(this).before($(this).data('fields').replace(regexp, time))
  event.preventDefault()
Community
  • 1
  • 1
AarCee
  • 833
  • 2
  • 11
  • 24
0

I have created a repo for nesting model forms from rails cast 196/197 compatible with rails 5.

Check it at link

Emmanuel Mtali
  • 4,383
  • 3
  • 27
  • 53