Rails ajax form not submitting from within another -
i have rails 3.2 ajax form creates new room hotel. new room ajax form works correctly on room index page, once embed new room ajax form on hotel edit page, form submitted without using ajax.
to create new room form, use following link on hotel edit.html.erb page:
<%#= link_to 'add room', new_room_path(:hotel_id => @hotel.id), :remote => true, :class => 'new_room' %> this loads following form partial on same page:
<%= form_for @room, :remote => true, :html => { :class => "remote-form" } |f| %> <%= f.text_field :number %> <%= f.text_field :size %> <% if(params.has_key?(:'hotel_id')) %> <% @hotel_id = params[:hotel_id] %> <%= f.hidden_field :hotel_id, :value => @hotel_id %> <% else %> <%= f.collection_select(:hotel_id, hotel.all, :id, :name) %> <% end %> <%= f.submit "add room", :class => 'room_create' %> <%= link_to 'cancel', '#', :class => "room_cancel" %> <% end %> and finally, have following in create.js.erb (inside rooms folder):
alert('test creating room'); var content = $('<%= escape_javascript(render(@room)) %>'); $("#room_list tbody").append(content); the create.js.erb not executed , form submitted regularly (non-ajax) , arrive on room show.html.erb page.
why form working correctly on units index page, not on associated hotel edit page?
even when set :remote => true, rails generates form tag. nested form tags not supported browsers , result in unpredictable behavior.
you should rethink views architecture here. can have forms rooms outside of form hotel, or maybe can use fields_for , accepts_nested_attributes_for edit children objects.
here's full example on how use nested attributes: nested attributes examples.
Comments
Post a Comment