Rails的3.1 AJAX:成功处理 [英] Rails 3.1 ajax:success handling

查看:116
本文介绍了Rails的3.1 AJAX:成功处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,进出口玩的CoffeeScript,Rails 3.1里所有的好东西。我与所有常见的航线指数,显示,创建,编辑,更新的资源,破坏。

So Im playing with CoffeeScript, Rails 3.1 all the good stuff. I have a resource with all the usual routes index, show, create, edit, update, destroy.

索引视图有一个使用表格:远程=>真正的像这样:

The index view has a form that uses :remote => true like so:

<%= form_for @todo, :remote => true do |f| %>
    <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %> 

在控制器创建我有以下几点:

In the controller for create I have the following:

def create
    @todo = Todo.new(params[:todo])

    respond_to do |format|
      if @todo.save
        format.html { redirect_to @todo, notice: 'Todo was successfully created.' }
        format.json { render json: @todo, status: :created, location: @todo }
        format.js {render json: @todo }
      else
        format.html { render action: "new" }
        format.json { render json: @todo.errors, status: :unprocessable_entity }
      end
    end
  end

我试着不使用.js.erb的意见,我宁愿处理JSON回来,做所有的花哨追加到待办事项列表等等。 (我就觉得更清洁的我)。

Im trying to not use .js.erb views as I would rather handle the JSON returned and do all the fancy appends to the todo list and so on. (It just feels cleaner to me).

在我todos.js.coffee我使用了以下内容:

In my todos.js.coffee I have used the following:

$(document).ready ->
    $("#new_todo")
      .bind "ajax:success", (event, data) ->
        alert("Ajax SUCCESS!!!")

(是刚tyting打开一个警告框,不工作),我试图负载,但就是无法触发此事件。请求并成功完成,新的待办事项被添加。

(Yeah just tyting to open an alert box does not work) I tried loads but just cannot trigger this event. The request does complete successfully and the new todo is added.

任何帮助,这将是AP preciated。谢谢

Any help with this would be appreciated. Thanks

推荐答案

开始倒在rails.js并想知道是否有任何的Ajax:回调正在提高。

Started to pour over the rails.js and wondered if any of the ajax: callbacks were being raise.

原来他们是很好的beforeSend和错误......挂在...错误?怎么会这样?新的待办事项的创建成功地发生,响应我期待JSON。但在通过回调code踩着我注意到一个无效的标签错误。

Turned out they were well the beforeSend and error... hang on... error? How could this be? The creation of the new todo happens successfully, the response is the JSON I expect. But on stepping through the callback code I notice an Invalid label error.

快速谷歌后来把我带到这个职位 http://blog.seqmedia.com/?p=484

Quick google later brings me to this post http://blog.seqmedia.com/?p=484

原来,JSON正在返回一个字符串,Firbug了这一点,正确地分析它,所以我可以检查响应。然而rails.js和一般的didnt JS知道如何处理字符串并抛出上述错误(而默默我可以这么说)。

Turns out the JSON is being returned as a string, Firbug got that and parsed it correctly so I could check the response. However rails.js and js in general didnt know how to handle the string and threw the above error (rather silently I may say).

的解决办法是在的respond_to

The solution was in the respond_to

format.js {render json: @todo, content_type: 'text/json' }

一个有点感谢特雷弗·伯纳姆(之类的书BTW)对他的帮助和艾米从序列媒体的博客文章,最终给了我溶液

A bit thanks to Trevor Burnham (like the book BTW) for his help and Amy from sequence media whose blog post ultimately gave me the solution.

这篇关于Rails的3.1 AJAX:成功处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆