views:

97

answers:

2

I have a form (snippet)

<% form_for(@transfer, :html => {:multipart => true}) do |f| %>
  <p>
    <%= f.label :source %><br />
    <%= f.text_field :source %>
  </p>
  <p>
    <%= f.label :destination %><br />
    <%= f.text_field :destination %>
  </p>

  <% fields_for :upload do |u| %>
    <p>
      <%= u.label :upload %><br />
      <%= u.text_field :upload %>
    </p>
  <% end %>

  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', transfers_path %>

So now in my transfers controller I can do:

@transfer = Transfer.new(params[:transfer])
@upload   = Upload.find_or_create_by_md5(params[:upload])

I am able to post to a single form with XML by simply changing the params to XML like

<transfer>
    <source>foo</source>
    <destination>bar</destination>
</transfer>

or

<upload>
    <upload>baz</upload>
</upload>

But I cannot figure out how to combine them under the same XML root

A: 

Assuming you're using Rails 2.3.x, you might want to look into adding accepts_nested_attributes_for to your Transfer model. See what-s-new-in-edge-rails-nested-attributes.

Jonathan Julian
Thanks for the pointer. I saw that, but my problem is that I don't necessarily want to create a new Upload for each Transfer. If that Upload already exists (by way of it's md5 attribute), I want to use an existing one. The next line in that code is:`@upload.transfers.push @transfer`
Fotios
A: 

Well I haven't been able to figure out how to do this for XML, so for now I've had to settle for doing it with REST. I came across the RestClient library and looking through the source, figured out you could do nested params like this:

RestClient.post( url, 
  { 
    :transfer => {
    :path => '/foo/bar',
    :owner => 'that_guy',
    :group => 'those_guys'
  },
  :upload => {
    :file => File.new(path)
  }
})

Send a note to the author about documenting the functionality here

Fotios