title: Rails 4: How to pass nested attributes with strong parameters into a model
body: <p>I've got two nested resources I'm working with here. Stashes &amp; Receivers. </p>
  
  <p><strong>routes.rb</strong></p>
  
  <pre><code>resources :stashes do
    resources :receivers
  end
  </code></pre>
  
  <p>I have a form where I'd like to create both a Stash and a Receiver once it's submitted. </p>
  
  <pre><code>&lt;%= form_for(@stash) do |f| %&gt;
    &lt;h2&gt;Who is this Stash for?&lt;/h2&gt;
    &lt;div class="reciever-wrap"&gt;
      &lt;%= f.fields_for @receiver do |builder| %&gt;
        &lt;div&gt;&lt;%= builder.label :first_name %&gt;&lt;br /&gt;
        &lt;%= builder.text_field :first_name, :autofocus =&gt; true %&gt;&lt;/div&gt;
  
        &lt;div&gt;&lt;%= builder.label :last_name %&gt;&lt;br /&gt;
        &lt;%= builder.text_field :last_name %&gt;&lt;/div&gt;
  
      &lt;% end %&gt;
     &lt;div class="self-check"&gt;
        &lt;%= check_box_tag(:self) %&gt;
        &lt;%= label_tag(:self, "This stash is for me") %&gt;&lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.label :occasion, "Is this stash for an occassion?" %&gt;&lt;br /&gt;
    &lt;%= f.text_field :occasion %&gt;&lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.label :initial_amount, "How much would you like to spend?" %&gt;&lt;br /&gt;
    &lt;%= f.text_field :initial_amount %&gt;&lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.label :length, "How long would you like this Stash to last?" %&gt;&lt;br /&gt;
    &lt;%= select_tag(:length, options_for_select([['3 Months', 1],['6 Months', 2],['1 Year']])) %&gt;    &lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.submit "Start adding gifts to this Stash!", :class =&gt; "button" %&gt;&lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I think that it makes the most sense to create the Receiver first, and then the Stash, so I can associate the Stash with the Receiver. So in the Stashes controller (because the Stash is primary in this form) I have this in the create action:</p>
  
  <p><strong>stashes_controller.rb</strong></p>
  
  <pre><code>def create
    receiver = Receiver.create_receiver(current_user, stash_params)
    if receiver.id
      @stash = Stash.create_stash(current_user, stash_params, receiver)
      if @stash.id
        flash[:success] = "Stash Started!"
        redirect_to stashes_url(@stash), :notice =&gt; "Stash Started!"
      else
        render :action =&gt; "new", :notice =&gt; "Error, try that again"
      end
    end
  end
  </code></pre>
  
  <p>I'm also defining the stash_params at the bottom:</p>
  
  <pre><code>def stash_params
    params.require(:stash).permit(
      :intitial_amount,
      :length,
      :user_id,
      :first_name,
      :last_name,
      :id,
      :receivers_attributes =&gt; [:id, :first_name, :last_name])
  end
  </code></pre>
  
  <p>I'm able to create the Stash no problem. But when I go to create the Receiver, I'm running into trouble. The system is creating a new Receiver record, but none of the data from the form is being stored. I'm guessing that it has something to do with not being able to access that nested strong parameter data. </p>
  
  <p>This is the create_receiver method in the Receiver model that should be saving the data, but for some reason isn't. I am able to save the current_user data, so that's good. It's only the data from the strong attributes that isn't being saved:</p>
  
  <p><strong>receiver.rb</strong></p>
  
  <pre><code>def self.create_receiver(current_user, stash_params)
    Receiver.create!(
      :first_name =&gt; stash_params[:first_name],
      :last_name =&gt; stash_params[:last_name],
      :user_id =&gt; current_user
    )
  end
  </code></pre>
  
  <p>Do I need to be traversing the stash_params to access the nested receiver_params attributes? I'm not sure how I should be accessing the nested attributes in the model. Any help is greatly appreciated! </p>
  
  <p>I also tried moving this create method into the Stash model to see if I could access the attributes from there, but I had no luck with that either.</p>