文章出處
文章列表
之前做。net的時候,自己做了一個showcontent的插件,用來加載頁面的局部partial
之前采用的是ashx的方式
但rails里面不太方面,今天找到一個比較好的方法,試驗成功
起初網上找到一個解決方案
HTML Tab <li class='StreamTab StreamTabRecent active'> <%= link_to 'Most Recent', mostrecent_schools_path, :remote => true, :class => 'TabText' %> </li> <div id='ContentBody'> <div id='ajax'></div> <%= render 'users/microposts', :microposts => @microposts %> </div> School Controller class SchoolsController < ApplicationController def mostrecent @school = School.find_by_slug(request.url.gsub('http://localhost:3000/','')).id @microposts = @user.microposts.order('created_at DESC').paginate(:per_page => 3, :page => params[:page]) respond_to do |format| format.html format.js end end end
Most Recent JS
$("#ContentBody").html('<%= escape_javascript(render :partial => "users/microposts" )%>');
Routes
resources :schools do
collection do
get :mostrecent
end
end
原文:http://stackoverflow.com/questions/17246411/ajax-call-to-render-partial-in-rails-3
但這個方法不太好,會有一些特殊字符過濾問題
后來發現這個方法不錯
def mostrecent @school = School.find_by_slug(request.url.gsub('http://localhost:3000/','')).id @microposts = @user.microposts.order('created_at DESC').paginate(:per_page => 3, :page => params[:page]) respond_to do |format| format.json { render :json => {:success => true, :html => (render_to_string 'users/microposts')} } format.html { } end end
$('.TabText').live 'ajax:success', (event,data) -> $('#ContentBody').html(data.html) if(data.success == true)
通過locals可以給partial傳直
render_to_string(:partial => 'my_partial', :layout => false, :locals => {:my_object => my_value})
利用這個方式可以做無刷新的分頁技術,而且相當方便
文章列表
全站熱搜