2010-06-30 4 views
0

실례지만, jQuery로 루비 바를 달성하는 방법을 이해할 수는 없습니다. 나는 json POSTS가 credit_status = 0을 만들기 위해 서버에 POSTS 버튼을 만들었지 만, 페이지에서도 자동 갱신을 원한다.레일, AJAX 및 jQuery .. 물을 추가 하시겠습니까?

내보기 :

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를 사용하여 페이지에서 단일 변수를 그리고 업데이트하는 방법을 알고 있습니까? 이 사실을 알게 된 훌륭한 자원이 있습니까?

답변

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는

는 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 %>"); 

* .html.erb와 같은 구문을 가진 다른 erb- 템플릿

+0

Worked! 나는 haml에서 이렇게하고있다. 그래서 내 것은 단순히 $ ("total_credit) .text ("# {@ organization.total_credit} "이다.) 또한 재미있다. format.js가 내 redeem 함수에 없다. . 레일즈는 자동적으로 레일스가 적절하게 명명 된 경우 기본적으로 .js 파일이라고 가정합니다. – Trip