views:

266

answers:

1

I'm trying to nest a form within another using submit_to_remote but it does a PUT instead of a POST. Can anyone explain what's wrong here?

The routes are RESTful:

map.resources :thing
map.resources :item

The view is like this:

<% form_for(@thing) do |f| %>
  <% fields_for(Item.new) do |i| %>
    <%= i.text_field :name %>
    <%= submit_to_remote 'create', 'Create', :url => items_path, :method => "post" %>
  <% end %>
  <%= f.text_field :title %>
  <%= f.submit 'Update' %>
<% end %>

To get around the problem I've been adding another method to the restful routes to do a create on a PUT but it's ugly and I want to know what the problem is.

The submit_to_remote comes out as:

<input name="create" onclick="new Ajax.Request('/items', {asynchronous:true, evalScripts:true, method:'post', parameters:Form.serialize(this.form) + '&amp;authenticity_token=' + encodeURIComponent('blah')});" type="button" value="Create">

Thanks

A: 

How about using link_to_remote instead and style the link to like a 'button', or just leave it as a link would be fine to be honest. This way you can control the XmlRequest fully. Currently I think the method is being determined by your actual form that is being submitted by the JS, not the :method your setting in the helper call.

tsdbrown
Thanks for the response. A link would do the trick. I am still curious though as to why the XmlRequest can't submit a POST in this scenario. I think you're right that it's using the method from the form. Perhaps it's a Prototype thing. I've not tried with jQuery.
Jo
No problem, try changing your form to be a post instead of a put (work on a new object for create) and see if that does it. At least then you'll know its the main form that is setting the method.
tsdbrown