Ruby on Rails的3,在于form_tag遥控器上的响应Ajax请求器上、on、Ruby、Rails

2023-09-10 16:00:12 作者:没有你/我死不了

感谢您阅读这篇文章。我一直停留在某个问题上与回报率在过去的几天。我有一个表格下index.html.erb为:

thanks for reading this post. I've been stuck on an issue with RoR for the past few days. I have a form under index.html.erb as:

<head>
    <title>Ajax List Demo</title>
    <%= javascript_include_tag :defaults %>  
    <%= csrf_meta_tag %>  
</head>
<body>
  <h3>Add to list using Ajax</h3>

  <% form_tag :action => :list , :method=>:get, :remote=>true  do %>
    Enter the public url:<%= text_field_tag 'url' ,'', :size => 80 %>
    <%= submit_tag "Find" %>
  <% end %>

  <div id="my_list">
  </div>
</body>

在控制器我有:

 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

和作为list.js.erb一个js文件进行远程UJS

And a js file for remote UJS as list.js.erb

$("#my_list").html("<%= escape_javascript(render(:partial => "list"))%>");

问题是我不能让结果来呈现局部_list.html.erb,在div标签my_list。我得到一个空白页面,406错误。如果我取消注释渲染HTML code控制器我得到的部分,在浏览器的后退呈现。我有点卡住,我想提交表单,结果弹出了my_list股利。我是新来的铁轨,所以如果我失去了一些东西明显不毫不犹豫地指出来我....我肯定愿意去尝试。

The issue is I cannot get the results to render the partial _list.html.erb, in the div tag my_list. I get a blank page, 406 error. If I un-comment the render html code in the controller I get the partial back rendered in the browser. I am kind of stuck, I want to submit the form and the results to pop in the my_list div. I'm new to rails so if I'm missing something obvious don't hesitate to point it out to me....I'm definitely willing to try.

改成了这样:

<html>
    <head>
        <title>Ajax List Demo</title>
        <h1>Listing posts</h1>
           <%= javascript_include_tag 'jquery.js' %>  
    <%= csrf_meta_tag %>  
    </head>
    <body>
        <h3>Add to list using Ajax</h3>

        <% form_tag :action => :doit , :method=>:get, :remote=>true  do %>
        Enter the public url:<%= text_field_tag 'url' ,'', :size => 80 %>
        <%= submit_tag "Find" %>
        <% end %>

        <div id="my_list">
                   </div>

控制器:

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(&LT;%= escape_javascript(渲染(:部分=>DOIT))%>);

JS _doit.js.erb $("#my_list").html("<%= escape_javascript(render(:partial => "doit"))%>");

和最后一个部分:

_doit.html.erb。

_doit.html.erb.

不过我仍然得到406错误,我没有一个重复的_doit JS或再培训局。做任何事情杰出从这个是不正确的?再次感谢!

However I am still getting the 406 error, I dont have a duplicate _doit js or erb. Does anything standout as incorrect from this? Thanks again!

另一个更新:

我觉得形式是不正确呈现:

I think the form is not rendered correctly:

此呈现:

<% form_tag :action => :doit , :remote=>true, :id => 'myform' do %>
        Enter the public url:<%= text_field_tag 'url' ,'', :size => 80 %>
        <%= submit_tag "Find" %>
        <% end %>

本:

<form accept-charset="UTF-8" action="/home/doit?id=myform&amp;remote=true" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="&#x2713;" />
<input name="authenticity_token" type="hidden" value="MLuau4hvfdGO6FrYCzE0c0JzwHhHKZqjmV49U673sK8=" />
</div>        Enter the public url:
<input id="url" name="url" size="80" type="text" value="" />

    <input name="commit" type="submit" value="Find" />



        <input name="commit" type="submit" value="Find" />

其加入我的远程标记和ID的查询字符串,这个心不是错了?

确定最终得到一个线索表格需要用括号括起来:

Its adding my remote tag and id to the query string, isnt this wrong?

Ok finally got a clue forms need to be bracketed:

 <%= form_tag( { :action => 'doit' }, :multipart => true, :remote=>true, :id => 'myform' ) do %>

确定最后的更新今晚: 现在,我得到的日志中:

Ok last update tonight: Now I get in the logs:

在周三10月27日22点40分55秒-0400 2010开始POST/家/ DOIT为127.0.0.1 这里!!!!   处理由HomeController的#DOIT为JS   参数:{提交=>查找,URL=>http://www.facebook.com/people/James-Stewart/653161299,authenticity_token=>MLuau4hvf dGO6FrYCzE0c0JzwHhHKZqjmV49U673sK8 =,UTF8=>Γ£O} 渲染家居/ _doit.html.erb(4.0ms) 渲染家居/ doit.js.erb(9.0ms) 完成200 OK在807ms(浏览次数:40.0ms | ActiveRecord的:0.0ms)

Started POST "/home/doit" for 127.0.0.1 at Wed Oct 27 22:40:55 -0400 2010 here!!!! Processing by HomeController#doit as JS Parameters: {"commit"=>"Find", "url"=>"http://www.facebook.com/people/James-Stewart/653161299", "authenticity_token"=>"MLuau4hvf dGO6FrYCzE0c0JzwHhHKZqjmV49U673sK8=", "utf8"=>"Γ£ô"} Rendered home/_doit.html.erb (4.0ms) Rendered home/doit.js.erb (9.0ms) Completed 200 OK in 807ms (Views: 40.0ms | ActiveRecord: 0.0ms)

我看到的JS和它说,它使我的js /分。然而我却一无所获上my_list股利。我的JS文件:

I see as JS and it says it renders my js/partial. However I am getting nothing on my_list div. My JS file:

$("#my_list").html("<%= escape_javascript(render(:partial => "doit"))%>");

我的html.erb形式的文件现已:

My html.erb form file has now:

<script$('#myform').bind('ajax:success', function(evt, data, status, xhr){
    xhr.responseText;
  });></script>

这就像形式什么也不做,这是一个好兆头,没有更多的406错误。我知道这是接近的,如果任何人都可以点什么,我需要做的,JS那将是巨大的,否则我会好好休息,并尝试tmrw。

Its like the form does nothing, which is a good sign, no more 406 error. I know this is close, if anyone can point what I need to do in the js that would be great otherwise I'll take a break and try tmrw.

好吧,我认为它得到一个响应返回只是不渲染,你指出的是这个问题昨天史蒂夫。

Ok I think its getting a response back just not rendering as you pointed out would be the issue yesterday Steve.

调试JS的Firebug的我知道我想要在DIV呈现的HTML,为这个:

Debugging the JS on Firebug I see the html I want rendered in the div, for this:

http://localhost:3000/javascripts/prototype.js?1285674435/event/seq/1

这意味着我想我得到了JS响应回来了。

Which means I think I am getting the JS response back now.

我有这个表单页面上:

<script>$('#myform').bind('ajax:success', function(evt, data, status, xhr){
    $('#my_list').html(eval(xhr.responseText));
  });</script>

检查说它不知道什么MyForm的是,但我把:在Rails code ID =>'MyForm的

Inspections say it doesnt know what myform is, but I put :id => 'myform' in the Rails code.

再次感谢所有,我得到一吨的帮助,在这里,我想分享一下我终于得到了它的工作回馈社会。

Again all thanks, I got a ton of help here and I want to share how I finally got it working back to the community.

在,js文件的方法DOIT(DEF,需要一个更好的控制器操作名称)是doit.js

The, js file for the method doit(def. need a better controller action name) is doit.js

在code最终是:

$("my_list").update("<%= escape_javascript(render(:partial => "doit"))%>");

由于某种原因把它当作#my_list不会在Firefox中,我不得不用萤火虫终于想出解决办法。

For some reason leaving it as #my_list wouldn't be found in firefox, I had to use firebug to finally figure this out.

显然,这是从下面建议的方式不同,我要去的地方的js脚本回的形式和删除.js.erb文件看看是如何工作的作品。我想我只呈现了部分在format.js回应?也是在那里每个人都找到写UJS文件的信息?我什么都不知道的语法的任何以$开始。

Obviously this is different from the way suggested below, and I am going to place the js script back into the form and remove the .js.erb file and see how that works works. I suppose I just render the partial in the format.js response? Also where does everyone find info on writing the UJS files? I know nothing about the syntax for anything starting with $.

再次感谢您的帮助,终于觉得我正在学习轨进展。

Again thanks for the help, finally feel like I am making progress on learning rails.

推荐答案

我张贴Hacker News上这个答案,但想通的堆栈溢出社区可能会受益,以及: - )

I posted this answer on Hacker News, but figured the Stack Overflow community might benefit as well :-)

在Rails 3中,JavaScript的司机都非常放手(即不显眼的)。您遇到的问题是,您的应用程序返回给浏览器的JavaScript字符串,但并没有什么在随后的执行中的JavaScript页​​面的上下文的页面。

In Rails 3, the javascript drivers are very hands-off (i.e. unobtrusive). The problem you're having is that your app is returning to the browser a string of javascript, but there is nothing in the page that is then executing that javascript in the context of the page.

UJS驱动程序rails.js结合的形式和环节与数据远程=真,这是什么:远程=&GT;真正的是干什么的,让他们远程提交他们的请求,但是这正是Rails的魔法停止。

The rails.js ujs driver binds to forms and links with data-remote=true, which is what the :remote => true is doing, to make them submit their requests remotely, but that is where the Rails magic stops.

好消息是,远程请求触发了一些事件可以绑定到,它给你访问服务器返回(在按下列顺序脱火)的数据:

The good news is that the remote requests fires off some events you can bind to, which give you access to the data returned by the server (which fire off in the following order):

  ajax:before
  ajax:loading
  ajax:success
  ajax:complete
  ajax:failure
  ajax:after

您需要将事件绑定到 AJAX:您的形式成功事件。所以,如果你的表单有id为MyForm的,你会希望您的网页上是这样的:

You need to bind an event to the ajax:success event of your form. So, if your form had the id "myform", you'd want something like this on your page:

  $('#myform').bind('ajax:success', function(evt, data, status, xhr){
    eval(xhr.responseText);
  });

xhr.responseText 是您的服务器的回报,所以这只是执行它的JavaScript。

xhr.responseText is what your server returns, so this simply executes it as javascript.

当然,这是正确的也结合了一些错误处理,以及故障事件。

Of course, it's proper to also bind to the failure event with some error handling as well.

我平时甚至不使用返回的JavaScript的action.js.erb方法,我只是有我的控制器呈现HTML部分,然后我有一个这样的页面绑定:

I usually don't even use the action.js.erb method of returning javascript, I just have my controller render the HTML partial, and then I have a binding like this in the page:

  $('#myform').bind('ajax:success', function(evt, data, status, xhr){
    $('#target-div').html(xhr.responseText);
  });

实际上,我在写这个完整的文章中间,但我希望这是足以让你去。

I'm actually in the middle of writing a full article on this, but hopefully this is enough to get you going.

编辑:我完成了一篇文章,充分解释的Rails 3远程链接和形式可能会有所帮助:

I finished that article, fully explaining remote links and forms in Rails 3. Maybe it will help:

轨道3远程链接和表格: 一个明确的指南

Rails 3 Remote Links and Forms: A Definitive Guide

 
精彩推荐
图片推荐