请原谅我的愚蠢,但我无法理解如何使用jQuery获得ruby vars。我做了一个按钮,由json POSTS到服务器,使credit_status = 0.但是我希望0也能在页面上自动更新。
我的观点:
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'))}");
控制器(不确定是否需要看到这个):
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_who_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;
});
有没有人知道如何使用jSon / jQuery / Rails在页面上绘制和更新单个变量?有没有非常好的资源来学习这个?
答案 0 :(得分:2)
应用程序/控制器/ 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
应用程序/视图/凭单/ 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 %>" );
要理解的重要一点是,redeem.js.erb只是另一个erb-template,其语法与* .html.erb相同