title: Saving nested model in Rails 4
body: <p>Kinda new to the Rails thing, in a bit of a spot.</p>
  
  <p>One of the models is dependent on the other in a has_many/belongs_to association.</p>
  
  <p>Basically, when creating a "Post" on my application, a user can also attach "Images". Ideally these are two separate models. When a user chooses a photo, some JavaScript uploads it to Cloudinary and the returned data (ID, width, height, etc) are JSON stringified and set on a hidden field.</p>
  
  <pre><code># The HTML
  = f.hidden_field :images, :multiple =&gt; true, :class =&gt; "image-data"
  
  # Set our image data on the hidden field to be parsed by the server
  $(".image-data").val JSON.stringify(images)
  </code></pre>
  
  <p>And of course, the relationship exists in my Post model</p>
  
  <pre><code>has_many :images, :dependent =&gt; :destroy
  accepts_nested_attributes_for :images
  </code></pre>
  
  <p>and my Image model</p>
  
  <pre><code>belongs_to :post
  </code></pre>
  
  <p>Where I'm lost is what to do with the serialized image data on the Post controller's create method? Simply parsing the JSON and saving it doesn't create the Image models with the data upon saving (and doesn't feel right):</p>
  
  <pre><code>params[:post][:images] = JSON.parse(params[:post][:images])
  </code></pre>
  
  <p>All of this essentially culminates to something like the following parameters:</p>
  
  <pre><code>{"post": {"title": "", "content": "", ..., "images": [{ "public_id": "", "bytes": 12345, "format": "jpg"}, { ..another image ... }]}}
  </code></pre>
  
  <p>This whole process seems a little convoluted -- What do I do now, and is there a better way to do what I'm trying to do in the first place? (Also are strong parameters required for nested attributes like this...?)</p>
  
  <p>EDIT:</p>
  
  <p>At this point I got this error:</p>
  
  <pre><code>Image(#91891690) expected, got ActionController::Parameters(#83350730)
  </code></pre>
  
  <p>coming from this line...</p>
  
  <pre><code>@post = current_user.reviews.new(post_params)
  </code></pre>
  
  <p>Seems like it's not creating the images from the nested attributes but it's expected to.
  (The same thing happens when :autosave is there or not).</p>