은 시작이다 ... JSON을 처리하는 방법의 좋은 레일 3.x의 예를 찾을 수 없습니다
=link_to "delete", "#{invitation_path(invitation)}.json", :method=>:delete, :remote=>true, :class=>"remove", :confirm=>'Are you sure you?'
내 리소스의 URL에 ".json"을 추가한다는 점에 유의하십시오. 이것은 매개 변수의 의미를 보려면 AJAX delete, google link_to의 예입니다. 원격 사용자가 HTTP 요청을 매개 변수로 설정하는 경우 remote : true로 설정합니다. 즉, 브라우저에서 AJAX 호출로 변환됩니다. http://www.alfajango.com/blog/rails-3-remote-links-and-forms/
: 혹시 AJAX의 결과에 대한 자세한 내용은 1 단계의 LINK_TO에 사용자가 클릭하면이 블로그 게시물을 볼 수있을 때 할 것입니다 브라우저를 호출 무엇인지 처리 할 수 있도록
둘째, 일부 자바 스크립트를 쓰기 내 사이트에서
예 : 당신은 내가 반환 된 JSON을 구문 분석하고 볼과 그 기반으로 페이지의 HTML을 변경할 수 있습니다
jQuery(function($) {
// create a convenient toggleLoading function
var toggleLoading = function() { $("#loading").toggle() };
$("#pending_invitation")
.live("ajax:loading", toggleLoading)
.live("ajax:complete", toggleLoading)
.live("ajax:success", function(event, data, status, xhr) {
var response = JSON.parse(xhr.responseText)
if (response.result == "ok") {
$(this).fadeOut('fast');
}
else {
var errors = $('<div id="error_explanation"/>');
errors.append('<h2>Pending invitation action error</h2><ul><li>' + response.error + '</li></ul>');
$('#new_invitation_error').append(errors)
}
});
});
. 이 js는 여기에 포함되지 않은 상위 뷰에 정의 된 CCS ID와 클래스를 사용합니다.
class InvitationsController < ApplicationController
respond_to :html, :json
# other methods here
# ...
def destroy
@invitation = Invitation.find(params[:id])
respond_to do |format|
if @invitation
@invitation.destroy
flash[:success] = I18n.t 'invitations.destroy.success'
format.json { render :json =>{:result => "ok", :message=>"Invitation #{params[:id]} was destroyed", :resource_id=>params[:id] } }
else
format.json { render :json => { :result=>"failed", :error=>"Cannot find Invitation #{params[:id]}", :resource_id=>params[:id] } }
end
end
end
end
희망이 도움말 :
지금 여기에 JSON을 뱉어 당신에게 자신의 컨트롤러를 작성하려는 경우
은 예입니다.
[이 Railscast] (http://railscasts.com/episodes/229-polling-for-changes) – Zabba
참고로 도움이되지만, 특히 JSON 예제를 찾고 있는데, getScript)'. 이 예제가 내가 필요로하는 것에 어떻게 적용되는지보기 위해 레일스에서 충분히 좋지는 않다 ... – sepiroth
나는 또한 이것을 현저하게 발견한다. 거기에 많은 예제가 있지만 그들은 모두 당신이'action.js.erb'을 사용하고자한다고 가정하고 나는 그렇지 않습니다. 나는 ** 당신이 똑같은 것을 찾고 있다고 생각합니다 **. 그렇다면 예를 찾고 답변하려고 노력할 것입니다. – cbmeeks