Webページを埋めるために余分なデータを読み込むためにユーザーがトリガーする(jquery click)ajax呼び出しを作成しています。レールコントローラ内でhtmlデータを返すか、JSONを返してajax関数でビルドする必要がありますか?または、Ajaxコールをレールで行うための良い方法がいくつかありますか?ここでRails、ユーザーがトリガーしたajax呼び出しとhtml出力のオプション
は、現時点ではcountries.html.hamlコードは(HTMLデータがコントローラで生成された)である:
$('#tab li').click(function(){
$('#tab li').removeClass('active');
$(this).addClass('active');
var first_l = $(this).attr("id")
$.ajax({
url: "/countries/get_flags",
type: "GET",
data: {first_letter: first_l},
complete: function() {
// called when complete
},
success: function(data) {
// called when succesful
$('#countries_container').html("");
$('#countries_container').append(data);
},
error: function(xhr,status,error) {
// called when error
}
});
});
とコントローラ:
def get_flags
@html = ""
@fetched_countries = Country.all(:conditions => "name like '#{params[:first_letter]}%'")
i = 1
@html = "<div class='row country-row'>"
@fetched_countries.each do |country|
if i % 5 == 0
@html += "<div class='row country-row'>"
i = 1
end
@html += "<div class='span3 '>"
@html += "<div class='country-name'>#{country.name}</div>"
@html += "<div class='country-image thumbnail'><img alt='#{country.id.to_s}' src='/assets/countries/x100/#{country.id.to_s}.png' /></div></div>"
if i % 4 == 0
@html += "</div>"
end
i = i + 1
end
render :text => @html
end
このような素晴らしい答え!どうもありがとうございました!私は間違いなくそのクラスを調べます。 –
さて、私は中止して「終了」声明を出し、回答を編集しました – RadBrad