ruby - How to get rails to use ajax to update an HTML table row after the db update? -
i have table of content multiple rows.
given row want able verify record , have reflected on screen changing "verify link" hyperlink plain text 'verified'.
seems common pattern others.
i have following markup:
...# (table stuff). = link_to 'verify', verify_link_path(:id => link.id), class: 'verify', remote: true
this (successfully) calls links controller 'verify_link' method:
def verify_link @link = link.find(params[:id]) # ignore additional verify logic now, set verified (when user clicks verify). @link.verified_date = time.now @link.save! respond_to |format| format.js end end
i know works... because database gets updated.
finally want update page (table-row-cell) reflect this. stuck.
the controller method calls following app/views/links/verify_links.js.erb
file:
$(function() { $(".verify") .bind("ajax:success",function(event,data,status,xhr) { this.text='verified'; alert("the link verified."); }); });
i know gets invoked... because can put alert("hello world");
@ start , gets displayed pop-up.
the part isn't working replacing 'verify' link plain text 'verified'. can't alert
work in there @ all. nothing gets updated in ui. on backend change in db made however, if refresh page see change reflected @ point).
notes:
- is there issue fact there multiple links? i.e. should using id, have munge table row id or number
- app version rails 3.2.17 asset pipeline.
- app written in rails 2 , later upgraded.
- i using jquery 1.7.2 , rails.js avoiding frameworks angular, ember, etc. want learn more of lower level stuff first. once use framework.
@hjing right. code inside app/views/links/verify_links.js.erb response ajax. need not bind ajax:success.
as suggested need make id each link you'll have munge table row id or number. example your links this:
= link_to 'verify', verify_link_path(:id => link.id), class: 'verify', id: "verify_#{table_row_no}", remote: true
then inside app/views/links/verify_links.js.erb can target particular link this:
$("#<%= j 'verify_#{table_row_no}' %>").text("verified"); $("#<%= j 'verify_#{table_row_no}' %>").prop("href","#"); #you want change href in case clicks again
Comments
Post a Comment