Rails 关于 ajax

sforce100 · December 06, 2012 · Last by simlegate replied at December 13, 2012 · 2524 hits

想通过 ajax 替换掉

里面得模板 load.html.erb: <%= link_to_remote t1.name, :url => attribute_path, :type_name1 => t1.name %> <%= render :partial => 'load', :collection => @default.attrs %>

controller 对应的 js.erb: $(“#attributes1”).html(“<%= escape_javascript render(:partial => 'load', :collection => @define1.attrs ) %>”);

controller: def attribute @define1 = ModelName.where(:name => params[:type]).first

respond_to do |format| format.html # index.html.erb format.js end end

不生效吖...哪里出现问题了么

用 ujs 吧,书上的东西太老了

你 js 文件怎么命名的

controller 对应的 js.erb: 我好像没有见过啊。 我见过的都是 actoin 对应的 js。 比如说 create.js.erb

将 action 替换为 ajax

#3 楼 @woaigithub 我是用 scaffold 生成的,生成 model_name.js.coffee,而且 remote 就对应了 response 的 format.js,现在提示 ActionView::MissingTemplate (Missing template admin/graph_relations/attribute

#1 楼 @vkill 想问下,ajax,请求到达控制器后怎么调用 js

link_to_remote 在 3.1 以后就不再支持了,改成:remote=> true

#7 楼 @Roam 这个我刚刚改掉了。。现在加了 attribute.js.erb...模版替换成空了..离成功不远了

#7 楼 @Roam $(“#attributes1”).html(“<%= escape_javascript render(:partial => 'load', :collection => @define1.attrs ) %>”);我这句有问题么

你的 load.html.erb 不是 partial,应该这样命名,_load.html.erb

#10 楼 @Roam 可以了。。我 2 了。。参数名写错了。。谢谢

这样不太好看,你可以多发点主要有联系的代码。你的主页面的 HTML 就是 load.html.erb 么?

Missing template admin/graph_relations/attribute 提示很清晰,没有模板,也就是没有 app/views/admin/graph-relations/attribute.html.erb 这个文件。

respond_to do |format|
  format.html # index.html.erb
  format.js
end

你的代码中支持了两种 response 类型,一种是 html,就需要这个模板文件,一种是 js,用来做 ajax 的。 如果你只有 ajax,那么就不需要 format.html 了,只保留 format.js 就可以了。

#1 楼 @vkill 不是 ujs 太老了,而是他在用老的 ujs

You need to Sign in before reply, if you don't have an account, please Sign up first.