I have a rails form where I would like to add the functionality to add more fields to to-many relations in nested form. I have a form which is like this;
= form_for @company do |f|
= f.label :name, "Company name"
= f.text_field :name
= f.label :homepage_url, "Company website"
= f.text_field :homepage_url, placeholder: "www.mycompanywebsite.com"
= render "domains_form"
%div{class: "add_more_button"}
= link_to "Add more domains", "add_domain", remote: true, data: {mini: "true", theme: "b", iconpos: "left", icon: "plus", role: "button"}, id:"more_email_domains"
= f.submit "Continue", data: {theme: "a", icon: "arrow-r", iconpos: "right"}
The partial "domain_form" looks like this;
= fields_for @company do |f|
= f.fields_for :domains do |d|
= d.label :identifier, "Domain name"
= d.text_field :identifier
This gives me the error as this;
undefined method `[]' for nil:NilClass
Extracted source (around line #2):
1: %div
2: = fields_for @company do |f|
It seems like partial could not find the @company instance but how could it be ? Since, the main template has the access to instance, shouldn't the partial also have access to instance. Do I need to pass the company instance as the locale exclusively.
The problem is more likely in fields_for call as I am not using it on any form_builder object rather using it as a global method. As rails api suggests that the method can be used standalone http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_form, such that the first fields_for takes parent object and the following one takes child, this should work fine on my assumption. Could you please help me with this.
Thanks