Rails 3.1 ajax:成功处理

发布于 2024-11-18 16:33:31 字数 1282 浏览 1 评论 0原文

所以我正在使用 CoffeeScript、Rails 3.1 等所有好东西。我有一个包含所有常用路线索引、显示、创建、编辑、更新、销毁的资源。

索引视图的形式使用 :remote => true 像这样:

<%= 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 %> 

在创建的控制器中,我有以下内容:

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 并将所有花哨的附加到待办事项列表等。 (对我来说感觉更干净)。

在我的 todos.js.coffee 中,我使用了以下内容:(

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

是的,只是输入打开警报框不起作用)我尝试了加载,但无法触发此事件。请求成功完成并添加新的待办事项。

任何对此的帮助将不胜感激。谢谢

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

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).

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

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

(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.

Any help with this would be appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2024-11-25 16:33:31

开始研究 Rails.js 并想知道是否有任何 ajax: 回调被引发。

事实证明,它们在 beforeSend 和 error... 等等... 错误方面都很好?怎么会这样?新待办事项的创建成功,响应是我期望的 JSON。但在单步执行回调代码时,我注意到一个 Invalid label 错误。

后来快速谷歌把我带到这篇文章 http://blog.seqmedia.com/?p=484

结果是JSON 作为字符串返回,Firbug 得到了它并正确解析了它,这样我就可以检查响应。然而,rails.js 和 js 一般不知道如何处理字符串并抛出上述错误(我可能会默默地说)。

解决方案在respond_to中,

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

感谢Trevor Burnham(顺便说一句,就像这本书)的帮助和来自sequence media的Amy,她的博客文章最终给了我解决方案。

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

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.

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

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).

The solution was in the respond_to

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

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.

倦话 2024-11-25 16:33:31
  1. 什么是#new_todo?具有该 ID 的元素是否存在?验证 $('#new_todo').length > 0 在您的代码中,并且它是绑定事件处理程序的正确元素。
  2. 什么是ajax:success? jQuery API 定义了一个 ajaxSuccess 事件(请参阅 Ajax 事件文档),但是我以前从未见过带冒号的版本。
  1. What is #new_todo? Does an element with that ID exist? Verify that $('#new_todo').length > 0 in your code, and that it's the right element to bind your event handler to.
  2. What is ajax:success? The jQuery API defines an ajaxSuccess event (see the Ajax Events docs), but I've never seen the version with the colon before.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文