Note that there are some explanatory texts on larger screens.

plurals
  1. PONested model form not submitting everything
    text
    copied!<p>I heard about this community while listening to Hypercritical and I am excited to join in with my first question. I am working on my first rails App and I have run into an issue that I cannot seem to crack. I have been watching Railscast, Lynda.com, and Googling for days but I still cannot comprehend how to create a form that that will update my has_many :through associations at once. Allow me to try an explain what I am doing.</p> <p><strong>My Goal:</strong> The firm I work for provides many "Service Offerings" and I want to be able to create a new service offering on one page and have it create the contacts and other information that is associated with it. The additional information such as "contacts" will live in their own tables because they may need to be referenced by many "Service Offerings."</p> <p><strong>Problem:</strong> When I submit the form the "Service Offering" fields submit and are entered into the database, but the fields for the "Business Developer" do not. Obviously, I would like everything to be entered into its appropriate table and the for the IDs to be linked in the join table. I would really appreciate any insight that you could provide. </p> <p><strong>What I Have So Far:</strong> What you see below is Service Offerings and Business Developers. Eventually I will be adding Contacts, Photos, and Files but I thought I would start simply and work my way up.</p> <p><strong>Models:</strong></p> <pre><code>class ServiceOffering &lt; ActiveRecord::Base attr_accessible :name, :description has_many :business_developer_service_offerings has_many :business_developers, :through =&gt; :business_developer_service_offerings accepts_nested_attributes_for :business_developer_service_offerings end class BusinessDeveloper &lt; ActiveRecord::Base attr_accessible :first_name, :last_name has_many :business_developer_service_offerings has_many :service_offerings, :through =&gt; :business_developer_service_offerings end class BusinessDeveloperServiceOffering &lt; ActiveRecord::Base belongs_to :business_developer belongs_to :service_offering end </code></pre> <p><strong>Controller:</strong></p> <pre><code>def new @service_offering = ServiceOffering.new @service_offering.business_developers.build end def create @service_offering = ServiceOffering.new(params[:service_offering]) if @service_offering.save redirect_to(:action =&gt; 'list') else render('new') end end </code></pre> <p><strong>View:</strong></p> <pre><code>&lt;%= form_for((@service_offering), :url =&gt; {:action =&gt; 'create'}) do |f|%&gt; &lt;p&gt; &lt;%= f.label :name%&gt; &lt;%= f.text_field :name %&gt; &lt;%= f.label :description%&gt; &lt;%= f.text_field :description %&gt; &lt;/p&gt; &lt;%= f.fields_for :business_developer do |builder| %&gt; &lt;p&gt; &lt;%= builder.label :first_name%&gt; &lt;%= builder.text_field :first_name %&gt; &lt;%= builder.label :last_name%&gt; &lt;%= builder.text_field :last_name %&gt; &lt;/p&gt; &lt;%end%&gt; &lt;%= f.submit "Submit" %&gt; &lt;%end%&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload