ajax - Correct way to have multiple submit buttons on single form in rails -
i have page in rails application requires me have multiple submit buttons on single form. give idea of trying do, have shopping cart, table of items. each item has order button, , remove button. there checkbox associated each item, multiple items can selected, , submitted order button @ bottom of table.
currently, each of these 3 buttons have different "name" property associated them. in controller have code similar following:
if params[:remove] #do stuff remove item shopping cart, re-direct shopping cart elsif params[:order_one] #do stuff order item, render confirm page else #do stuff order multiple items, render confirm page end but seems messy me. there better "railsy" way implement this?
also, not using js/ajax in pages, , have shopping cart div rendered via ajax. when user clicks remove button, form submitted remotely, shopping cart div can re-rendered via ajax once item removed shopping cart - possible having 1 form has button submitting forms via ajax, , forms without ajax?
this vast subject, ajax delete button can start this. in table view, use :
<tr id="tr_<%= item.id %>"> ... <%= link_to 'delete', item, method: :delete, remote: true %> </tr> you must handle ajax call on destroy route in items_controller.rb:
def destroy @item= item.destroy(params[:id]) respond_to |format| format.html { redirect_to items_url } format.js end end create new destroy.js.erb file in items views folder , put in :
$('#your_table').remove('#tr_<%= @item.id %>'); then when click on remove button, call destroy action in controller. button defined remote, rails automagically perform ajax request , won't reload page. controller render according js view in remove row table.
as said, beginning , can done better on javascript side. note didn't check code.
also have @ http://railscasts.com/episodes/136-jquery-ajax-revised?view=asciicast more in depth explanation.
Comments
Post a Comment