Rails, AJAX, and jQuery..just add water?
Excuse my daftness, but I can not understand how ruby vars can be attained with jQuery. I made a button that by json POSTS to the server to make a credit_status = 0. But I would like that 0 to also auto-update on the page as well.
My View:
HQ $
= organization.total_credit
= link_to 'redeem', redeem_admin_organization_path(organization), :class => 'button_short redeem live'
redeem.js :
== $("#organization_#{@organization.id} .redeem").html("#{escape_javascript(link_to('redeem', redeem_admin_organization_path(@organization), :class => 'button_short live redeem'))}");
controller ( not sure if you need to see this ):
def redeem
@organization = Organization.find(params[:id])
users_who_promoted = CardSignup.find(:all).select {|c| c.store_id == @organization.id && c.credit_status == true}
unless users_who_promoted.empty?
users_wh开发者_如何转开发o_promoted.update_all("credit_status","false")
end
@organization.update_attribute('total_credit', '0')
end
main.js:
$(".live").live("click", function() {
$.ajax({type: "GET", url: $(this).attr("href"), dataType: "script"});
return false;
});
Does anyone know how to draw and update a single variable on a page using jSon/jQuery/Rails? Is there a really good resource out there for learning this?
app/controllers/vouchers_controller.rb
class VouchersController < ApplicationController # Just made up the name
def redeem
@organization = Organization.find(params[:organization_id])
@organization.update_attribute('total_credit', 0)
respond_to do |format|
format.js
end
end
end
app/views/vouchers/redeem.js.erb
$("#organization_<%= @organization.id %> .redeem").html(
"<%= escape_javascript(link_to('redeem',
redeem_admin_organization_path(@organization),
:class => 'button_short live redeem')) %>");
$(".total_credit").text( "<%= @organization.total_credit %>" );
The important thing to understand is that redeem.js.erb is just another erb-template with the same syntax as in *.html.erb
精彩评论