【发布时间】:2012-05-14 11:57:23
【问题描述】:
我花了一天的时间试图了解 Rails :remote=>true 的工作原理。下面的问题似乎很复杂,但我试图用我提供的信息来理解这个简单的问题:
如何在不使用 :remote=>true 的情况下进行仅呈现为 JS 的 Ajax 调用? 据我了解 :remote=>true 只是生成并处理 AJAX 调用:
我的观点是这样的:
- 以一种非常复杂的方式选择,使用 :remote => true 创建一个链接。为简单起见,此处省略
.e_list = opts.sort_link(:name) = opts.sort_link(:description) - e_list.each do |e| .entry = link_to(e.name, '#', class: 'select_e', data: {e_id: e.id}) = e.description = paginate e_list, remote: true, params: {search:"j", com_id: com.id}
Gallery.js.erb
$('#com<%= @com.id %>').replaceWith('<%= escape_javascript render(partial: "shared/com", locals: {com: @com}) %>');
我的控制器:
def gallery
if params[:com_id]
@com = @s.com.find(params[:com_id])
@com.filter = params
end
if c = @s.com.where(:_type => "Com").first
@current_e = c.entries(@user.app_id).first
@current_e.og_url = view_context.og_url(@current_e)
end
render :text => "foobar" if !@current_e
end
日志,用户点击分页链接或排序链接后(关键是那些链接有:remote => true)
Started GET "super long url" for 127.0.0.1 at 2012-05-04 16:08:42 -0700
Processing by CController#gallery as JS
所以我尝试使用 AJAX 重新创建它:
$('button.search').live 'click', (e) ->
search = $(e.target).attr('search-term')
success_callback = (results) ->
if results
console.log(results)
update_components(results[0].entry, '.entry')
else
$.ajax(
url: 'super long url that is exactly the same url as above!'
).done ->
return false
我没有呈现为 JS 的失败响应,但我认为 :remote => true 只是一个 ajax 调用 wtf?:
Started GET "super long url identical as the one that renders JS" for 127.0.0.1 at 2012-05-04 16:07:22 -0700
Processing by ContestController#gallery as */*
发生了什么事?如何在不使用 :remote=>true 的情况下进行简单呈现为 JS 的 Ajax 调用?
【问题讨论】:
-
不。入门GET “/屏幕/ 4fa02763dc1c82269c0001da /比武/画廊?app_row_id = 5&COMPONENT_ID = 4f9f0305dc1c8255a300006c&PAGE_ID = 136755716356584&screen_only = 1&screen_permanent_id = 4f9edb0cdc1c8228800000ef和搜索= J&source_component_id = 4fa0228edc1c82269c00012c” 为127.0.0.1于2012-05-04 16点07分22秒-0700处理由ContestController#画廊as / 每个 url 都很长,但是完全相同。
-
试试
/screens/4fa02763dc1c82269c0001da/contest/gallery.js?app_row_id=5...。如果您希望响应中的 js 在浏览器中执行,您应该需要执行类似eval(response)的操作,但我只是建议,我从未这样做过,甚至知道如何在 javascript 中评估字符串的代码。跨度> -
...如果您将此作为答案提交,我将接受。
-
完成了,很高兴能帮上忙。是
.js对吧?
标签: ruby-on-rails ruby ajax render