简体   繁体   中英

Render partial form on page with another controller in Rails with jQuery

On my main page I have a search form that is submitting to the search-controller. On the same page I have another form for registering new resources that is submitting to the resources-controller. The resource form is rendered as a partial, with the form_for tag inside the partial. Everything works fine if the validation is successful, but when it's not, and the form is rerendered with jQuery, the submit button on the resource form no longer submits to the resources-controller, but to the search-controller. Is this because the form_for properties are set when the DOM is loaded when the page is rendered initially? Is there a way to make this work? Is there something like the .live()-function that can be used in this case?

Here is how the partial is initially rendered in the search/index.html.erb file:

<div id="new_resource_form" style="display:block">
  <%= render "resources/resource_form" %>
</div>

This is the form_for part of the resource_form partial:

<%= form_for @resource, :url => {:controller => 'resources', :id => @resource.id }, :remote => true do |f| %>

The jQuery used to rerender the partial is like this:

$('#new_resource_form').html('<%= escape_javascript raw render 'resources/resource_form' %>')

When you click on a submit button the click event bubbles until it reaches the surrounding form and the form submits - the submit button doesn't actually 'point' to anything. So it sounds as if the problem you are having is that when the form is re-rendered, the html is placing the submit button outside the resource form and inside the search form. Is there perhaps an error in your template html that the browser is trying to fix?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM