简体   繁体   中英

Rails 4 ajax load partial into view

there is a way to put an input dynamically from button? Example:

this is the form:

    <%= form_for(@order) do |f| %>

. ...

  <div class="field">
    <%= f.label :productlist %><br>
    <%= f.select :productlist, @products, :prompt => 'Select one!' %>
    <%= link_to 'Add More', '#', id: 'products-link' %>
  </div>
  <div id="newproduct">
    <p>load here the new input on Add more link</p>
  </div>
  <div class="field">
    <%= f.label :status %><br>
    <%= f.text_field :status %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

so i thinked up to ajax and a way to load a partial input into the form, but im not understanding the correct way to do this.

Thank you

In your javascript file add:

  $("a#products-link").click(function(){
    $.get('/partials/products_form', function(data){
      $("div#newproduct").append(data);
    }, 'html');
  });

Essentially what is happening is that you are going to send a get request via ajax to a form in /partials/products_form or wherever you choose to keep your form partial. And then this html will be appended to whatever div/identifier you choose in this case div#newproduct .

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