在rails中 Rendering Partials through Ajax
AJAX Through Rails rendering
2023-09-11 14:21:35 时间
之前做。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})
利用这个方式可以做无刷新的分页技术,而且相当方便
相关文章
- ajax 406 Not Acceptable
- ajax的工作原理
- 使用PHP和AJAX制作日历
- Ajax基本案例详解之$.ajax的实现
- ajax异步上传文件FormDate方式,html支持才可使用
- jquery.ajax 跨域请求webapi,设置headers
- ajax结合mysql数据库和smarty实现局部数据状态的刷新
- [RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through
- 前后端交互模式大总结 艾提拉 总结 attilax总结 目录 1. 通过ajax ajax就是js的网络api 完全解耦合 推荐1 1.1. Query Ajax 操作函数1 1.2. 服务
- Atitit 前后端交互模式 目录 1.1. Ajax1 1.2. Fetch api1 1.3. 服务端脚本模式(简单快速)1 1.4. 浏览器注入对象、函数1 1.5. 浏览器插件模式
- Atitit js通讯技术 jsbridge ajax bomext Atitit jsbridge 与jsrpc 的联系与区别 JSBridge——Web与Native交互 侧重本
- Ajax请求访问action推断文件是否存在
- AJAX入门---点滴的积累
- ZendFramework2学习笔记 json和ajax
- AJAX - onreadystatechange 事件
- PHP允许AJAX跨域请求的两种方法