新手问题 局部刷新 的一点问题求解

renyijiu · January 04, 2017 · Last by renyijiu replied at January 04, 2017 · 1555 hits

1、首先我在Users#index中返回了

format.js { render 'users/index', locals: {users: @users}}

2、在index.js.erb中的代码是这样的

$('#users-index-table > table').replaceWith("<%= escape_javascript(render(partial: 'users/users_index_table', locals: {users: users})) %>");

3、在_users_index_table.html.erb这里面写了一些 table 的具体数据

现在的问题是,操作 table 后,其中数据能够正常的替换掉,页面也显示,但是点击浏览器的后退,然后再前进,浏览器显示的是

$('#users-index-table > table').replaceWith("<table class=\"list aui\">\n  <thead><tr>\n......

这样子的js.erb中的代码,我自己看着觉得是没有问题的,因为我自己 render 确实是这个片段的代码,返回的也是正确的,

但是请教下,有没有什么办法或者正确的姿势能够显示页面,而不是这个 html 代码?

UPDATE

在发起请求的那个按钮或者链接添加format: :js,会产生

http://127.0.0.1:3000/users.js?.......

这样类似的请求,与另外的请求不同,可以解决这个问题。

event => page:load

你这里为何要用 JS 的方式插入 users_index_table 的内容,而不是直接服务端渲染呢?

#2 楼 @huacnlee 你的意思是在服务端渲染整个页面?用 js 插入的话,我只是想把 table 这个区域给局部更新了

#1 楼 @leiz_me 这个是在 js.erb 中添加这个?

Ajax 最传统的做法。

#5 楼 @hww 这种写法就没有什么办法解决了嘛?只能是自己去写个 ajax 请求然后局部替换掉?

renyijiu closed this topic. 04 Jan 19:53
You need to Sign in before reply, if you don't have an account, please Sign up first.