javascript - Rails 4: dynamically added checkboxes are not submitting -


i started learn rails , try apply ajax records added dynamically in listing. want delete multiple records selected via checkboxes directly in listing.

it works fine not when create new record , want delete without refreshing page. header's response : "activerecord::recordnotfound in accountscontroller#destroym". acts if click button without checking checkboxes.

thanks :)

accounts_controller.rb

def destroym   account.destroy(params[:delete])   respond_to |format|     format.html {redirect_to accounts_path}     format.js #render accounts/destroym.js.erb   end end 

_row.html.erb

<% @data = account.find(id) %>  <tr id='tr<%= @data.id %>'>   <td><%= @data.id %></td>   <td><%= @data.login %></td>  <td><%= check_box_tag 'delete[]', @data.id  %></td>  </tr>

index.html.erb

<table class="table table-striped table-hover " id="accountslisting">                <%= form_tag destroym_accounts_path, method: :delete, remote: true, id: "deleteform" %>                  <thead>                    <tr>                      <th>#</th>                      <th>username</th>                      <th>password</th>                      <th>website</th>                      <th>date</th>                      <th>editer</th>                      <th><%= submit_tag "delete" %>                      <%= link_to "delete", '', :onclick => "$('#deleteform').submit()", id: "removepwd", class: "text-danger", remote: true, method: :delete %> </th>                    </tr>                  </thead>                  <tbody>                  <% @accounts.each |account| %>                    <%= render 'row', id: account.id %>                  <% end %>                  </tbody>                <% end %>              </table> 

destroym.js.erb

$("#accountslisting input:checked").closest("tr").remove(); 

create.js.erb

$("#accountslisting tr:first").after("<%= escape_javascript (render 'row', id:@account.id) %>"); 

the problem in case you're replacing whole page's html... so, dom events bound elements destroyed.

when replace html (the .js.erb file), rails provide special attributes elements (rails calls "unobstrusive javascript", because there's no js code directly in elements).

the issue here, of course, js code rails uses (see link) applied when page launches.

you have several choices:

1) destroy.js.erb can remove exact row deleted (instead of replacing rows).

2) can call rails-ujs' code yourself. hack , should not considered.

3) can reload page "automatically" (you lose benefit of ajax).

4) can write own js code send ajax request.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -