Ruby On Rails 3,form_tag远程响应Ajax请求
感谢阅读这篇文章。 过去几天我一直坚持使用RoR的问题。 我在index.html.erb下面有一个表单:
Ajax List Demo Add to list using Ajax
:list , :method=>:get, :remote=>true do %> Enter the public url: 80 %>
在控制器中我有:
def list puts "here!!!!" reader = Reader.new @profiles = reader.processURL(params[:url]) #profileList = respond_to do |format| #format.html { render :partial=>true, :locals => { :profiles => @profiles}}#{ render :partial=>'profiles/list',:layout => false, :locals => { :profiles => @profiles}} format.js {render :content_type => 'text/javascript', :locals => { :profiles => @profiles}} # index.html.erb # format.rss render :partial=>'profiles/list',:layout => false, :locals => { :profiles => @profiles} end
并将远程UJS的js文件作为list.js.erb
$("#my_list").html(" "list"))%>");
问题是我无法在div标签my_list中获得结果来呈现部分_list.html.erb。 我得到一个空白页面,406错误。 如果我取消注释控制器中的渲染html代码,我会在浏览器中渲染部分返回。 我有点卡住了,我想将表单和结果提交到my_list div中。 我是新手,所以如果我错过了一些明显的东西,请毫不犹豫地向我指出……我绝对愿意尝试。
将其更改为:
Ajax List Demo Listing posts
Add to list using Ajax
:doit , :method=>:get, :remote=>true do %> Enter the public url: 80 %>
控制器:
def doit puts "here!!!!" reader = Reader.new @profiles = reader.processURL(params[:url]) respond_to do |format| # format.html {render :partial=>true, :locals => { :profiles => @profiles}}#{ render :partial=>'profiles/list',:layout => false, :locals => { :profiles => @profiles}} format.js #{render :content_type => 'text/javascript', :locals => { :profiles => @profiles}} # index.html.erb # format.rss render :partial=>'profiles/list',:layout => false, :locals => { :profiles => @profiles} end
JS _doit.js.erb $(“#my_list”)。html(“”doit“))%>”);
最后是部分:
_doit.html.erb。
但是我仍然得到406错误,我没有重复_doit js或erb。 有什么突出的不正确吗? 再次感谢!
另一个更新:
我认为表单没有正确呈现:
这呈现:
:doit , :remote=>true, :id => 'myform' do %> Enter the public url: 80 %>
这个:
它将我的远程标签和id添加到查询字符串中,这不是错的吗?
好的,最后得到一个线索forms需要括号:
'doit' }, :multipart => true, :remote=>true, :id => 'myform' ) do %>
好的最后一次更新今晚:现在我进入日志:
在10月27日星期三22:40:55 -0400 2010年开始POST“/ home / doit”为127.0.0.1 !!!! 由HomeController处理#doit作为JS参数:{“commit”=>“Find”,“url”=>“http://www.facebook.com/people/James-Stewart/653161299”,“authenticity_token”=>“ MLuau4hvf dGO6FrYCzE0c0JzwHhHKZqjmV49U673sK8 =“,”utf8“=>”Γ£ô“}呈现回家/ _doit.html.erb(4.0ms)渲染回家/ doit.js.erb(9.0ms)完成200 OK,807ms(浏览次数:40.0ms) | ActiveRecord:0.0ms)
我看作JS,它说它呈现我的js / partial。 但是我在my_list div上什么都没得到。 我的JS档案:
$("#my_list").html(" "doit"))%>");
我的html.erb表单文件现在有:
它就像表格什么都不做,这是一个好兆头,没有406错误。 我知道这很接近,如果有人可以指出我需要在js做什么,那将是很好的否则我会rest并尝试tmrw。
好吧,我认为它得到的回复只是没有像你指出的那样呈现昨天史蒂夫的问题。
在Firebug上调试JS我看到我想要在div中呈现的html,为此:
http://localhost:3000/javascripts/prototype.js?1285674435/event/seq/1
这意味着我认为我现在正在收到JS回复。
我在表单页面上有这个:
$('#myform').bind('ajax:success', function(evt, data, status, xhr){ $('#my_list').html(eval(xhr.responseText)); });
检查说它不知道myform是什么,但我在Rails代码中输入:id =>’myform’。
再一次感谢,我在这里得到了很多帮助,我想分享一下我最终如何让它回到社区。
方法doit(def。需要更好的控制器动作名称)的js文件是doit.js
代码最终是:
$("my_list").update(" "doit"))%>");
由于某些原因,在Firefox中找不到#my_list,我不得不使用firebug来最终解决这个问题。
显然这与下面建议的方式不同,我将把js脚本放回到表单中并删除.js.erb文件,看看它是如何工作的。 我想我只是在format.js响应中渲染部分? 还有谁在哪里找到有关写UJS文件的信息? 我对以$开头的任何语法都一无所知。
再次感谢您的帮助,最后感觉我在学习rails方面取得了进展。
我在Hacker News上发布了这个答案,但认为Stack Overflow社区也可能受益:-)
在Rails 3中,javascript驱动程序非常不干净(即不引人注目)。 您遇到的问题是您的应用程序返回浏览器中的一串javascript,但页面中没有任何内容在页面上下文中执行该javascript。
rails.js ujs驱动程序使用data-remote=true
绑定到表单和链接,这是:remote => true
正在执行的操作,以使它们远程提交请求,但这就是Rails魔法停止的地方。
好消息是远程请求会触发您可以绑定的某些事件,这些事件可以让您访问服务器返回的数据(按以下顺序触发):
ajax:before ajax:loading ajax:success ajax:complete ajax:failure ajax:after
您需要将事件绑定到表单的ajax:success
事件。 因此,如果您的表单的ID为“myform”,那么您需要在页面上显示以下内容:
$('#myform').bind('ajax:success', function(evt, data, status, xhr){ eval(xhr.responseText); });
xhr.responseText
是您的服务器返回的内容,因此只需将其作为javascript执行即可。
当然,也可以通过一些error handling来绑定故障事件。
我通常甚至不使用action.js.erb方法返回javascript,我只是让我的控制器呈现HTML部分,然后我在页面中有这样的绑定:
$('#myform').bind('ajax:success', function(evt, data, status, xhr){ $('#target-div').html(xhr.responseText); });
我实际上正在撰写关于此的完整文章,但希望这足以让你前进。
编辑:我完成了那篇文章,完整地解释了Rails 3中的远程链接和表单。也许它会有所帮助:
Rails 3远程链接和表单:权威指南
如果查看页面的渲染源,您应该注意到fields属性中的错误。
正确的方法如下:
<%= form_tag({:action => 'list'}, :remote => true %>
注意大括号,非常重要! 或者你可以做到:
<%= form_tag('list', :remote => true %>
你有2个名为’_list’的部分? 也许这会导致问题,你应该更具体一点:
$("#my_list").html("<%= escape_javascript(render(:partial => "list.html.erb"))%>");
我不确定这是否有帮助,但如果你在IE中使用,请注意IE会发送一些与控制器响应方式相关的标题。 因此,您可能正在向IE发送Ajax请求,但您的Rails应用程序认为它只是一个简单的HTML请求。
我必须设置jQuery来首先擦除当前标头,然后只添加javascript标头:
$.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept",'');xhr.setRequestHeader("Accept", "text/javascript")}
})
在控制器中使用list
作为函数名可能是问题所在。 该名称由Rails内部使用。
- 如何在Rails 4 App中启用CORS
- Ajaxified Star Rating System未正确更新
- 用于创建ajax关系的关系控制器-NoMethodError:未定义的方法` _url’
- 如何使Javascript生成的复选框保持不变?
- 如何在同一视图中呈现两个分页和可共享的集合?
- 保护AJAX API以防止其他网站利用它
- Rails form_for with file_field and remote => true and format =>:js
- 存储HTML5地理位置数据
- Rails表单:使用AJAX基于其他collection_select值更新collection_select选项