Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3.2 build_association is null'ing out my foreign key when the form loads
    text
    copied!<p>I think I need a little help understanding how a simple Rails 3.2 one-to-one association with a nested form works. I have a points form and when I visit it for the first time it loads with everything empty. And when I save everything looks good, it even has the right foreign key. Now, when I try to edit this record again by loading the form, the foreign key (results.pickem_id) gets nulled out and nothing loads in the form. It gets nulled the second <code>@pickem.build_result</code> is called. What do I need to do to allow editing of this form? I feel like I'm on the right track here so hopefully I'm missing something simple. I've included just the relavant code snippets but can provide more if need be.</p> <p>Pickem.rb</p> <pre><code> attr_accessible :result_attributes has_one :result accepts_nested_attributes_for :result </code></pre> <p>Result.rb</p> <p><code>belongs_to :pickem</code></p> <p>pickems_controller.rb</p> <pre><code> def update @pickem = Pickem.find params[:id] if @pickem.update_attributes params[:pickem] redirect_to pickem_path(@pickem), :notice =&gt; 'The pickem has been successfully updated.' else render "edit" end end def results #This is the results form @pickem = Pickem.find params[:id] @pickem.build_result #:pickem_id =&gt; params[:id] end </code></pre> <p>results.html.erb</p> <pre><code>&lt;%= simple_form_for @pickem, :html =&gt; {:class =&gt; "form-horizontal"} do |f| %&gt; &lt;%= f.simple_fields_for :result do |r| %&gt; &lt;%= r.input :pickem_id, :input_html =&gt; { :value =&gt; params[:id] }, :as =&gt; 'hidden' %&gt; &lt;%= r.input :first_name, :label =&gt; 'First Name:' %&gt; &lt;% end %&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