we have an requirement where all the products are displayed with the checkbox next to each product name.
index.html.erb
<%= form_tag users_path, :method => 'get' do %>
<table>
<tr>
<td><input type="checkbox" name="cb1", id="cb1" /> </td>
<td> Mobiles </td>
</tr>
<tr>
<td><input type="checkbox" name="cb2", id="cb2" /> </td>
<td> Laptops </td>
</tr>
<tr>
<td>
<%= link_to 'Add User', "/users/new", :method => :get, :target => "_blank"%> </td>
<td>
<%= link_to 'Delete User(s)', :method => :delete, :onclick =>
> "onDeleteClick();" %>
</td>
</tr>
</table>
<% end %>
i have got an javascript function which gets me the list of products selected and i am using substring of id i.e. if cb1 is the id of checkbox then 1 is the product id and am forming the list of product id's as 1,2,3.
ProductsController.rb delete action in the products controller.
def delete
User.delete( params[:selecteduserids])
render :action => "index"
end
Note: <%= hidden_field_tag :selecteduserids, params[:selecteduserids] %>
Javascript:
function onDeleteClick()
{
var objlist = document.getElementById('dvproductdetails');
var aList = objlist.getElementsByTagName('input');
var nLen = aList.length;
var nSelCnt = 0;
var arSelectedProductIDs = new Array();
for (var n=0; n<nLen; n++)
{
var oChk = aList[n];
if (oChk.type == 'checkbox' && oChk.id.substring( 0, 2) == 'cb' && oChk.checked)
{
arSelectedProductIDs[nSelCnt] = oChk.id.substring( 2, oChk.id.toString().length);
nSelCnt = nSelCnt + 1;
}
}
if( arSelectedProductIDs.length > 0)
{
document.getElementById('selectedproductids').value = arSelected开发者_开发问答ProductIDs.toString();
//alert(arSelectedProductIDs.toString());
return true;
}
else
{
alert('Please select the Product to delete.');
}
return false;
}
Issue: When i click on Delete link, the action is not executed. Can anyone guide me in getting solved the issue?
As Marc said, it would be helpful to see the HTML/Javascript responding to the remote delete action. In the meantime, here is a working implementation:
VIEW:
<%= link_to 'Destroy', model_path( model.id ), { :remote => true, :confirm => "#{translate( :are_you_sure )}", :method => :delete, :class => 'button' } %>
Generated HTML:
<a href="/model/1" class="button" data-confirm="Are You Sure?" data-method="delete" data-remote="true" rel="nofollow">Destroy</a>
Javascript required:
JQuery
JQuery-UJS
CONTROLLER:
def destroy
model = Model.find( params[ :id ] )
model.destroy
respond_to do | format |
logger.info { "DESTROY: #{format}" }
format.html { render :layout => false, :inline => "#{model.id}" }
format.json { head :ok, data => model.id }
end
end
EDIT
Also:
$("input:checkbox[name=type]:checked").each(function()
{
jQuery.ajax( "/model/1/delete" );
});
精彩评论