<bdo id='rhnZ0'></bdo><ul id='rhnZ0'></ul>
  • <i id='rhnZ0'><tr id='rhnZ0'><dt id='rhnZ0'><q id='rhnZ0'><span id='rhnZ0'><b id='rhnZ0'><form id='rhnZ0'><ins id='rhnZ0'></ins><ul id='rhnZ0'></ul><sub id='rhnZ0'></sub></form><legend id='rhnZ0'></legend><bdo id='rhnZ0'><pre id='rhnZ0'><center id='rhnZ0'></center></pre></bdo></b><th id='rhnZ0'></th></span></q></dt></tr></i><div id='rhnZ0'><tfoot id='rhnZ0'></tfoot><dl id='rhnZ0'><fieldset id='rhnZ0'></fieldset></dl></div>

  • <small id='rhnZ0'></small><noframes id='rhnZ0'>

    <tfoot id='rhnZ0'></tfoot>
      <legend id='rhnZ0'><style id='rhnZ0'><dir id='rhnZ0'><q id='rhnZ0'></q></dir></style></legend>

        Rails 3.1 ajax:成功处理

        Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
          <bdo id='VyY30'></bdo><ul id='VyY30'></ul>

            • <small id='VyY30'></small><noframes id='VyY30'>

              <tfoot id='VyY30'></tfoot>
                <tbody id='VyY30'></tbody>

              • <i id='VyY30'><tr id='VyY30'><dt id='VyY30'><q id='VyY30'><span id='VyY30'><b id='VyY30'><form id='VyY30'><ins id='VyY30'></ins><ul id='VyY30'></ul><sub id='VyY30'></sub></form><legend id='VyY30'></legend><bdo id='VyY30'><pre id='VyY30'><center id='VyY30'></center></pre></bdo></b><th id='VyY30'></th></span></q></dt></tr></i><div id='VyY30'><tfoot id='VyY30'></tfoot><dl id='VyY30'><fieldset id='VyY30'></fieldset></dl></div>

                  <legend id='VyY30'><style id='VyY30'><dir id='VyY30'><q id='VyY30'></q></dir></style></legend>
                1. 本文介绍了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.

                  索引视图有一个使用 :remote => 的表单.真的 像这样:

                  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!!!")
                  

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

                  (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

                  推荐答案

                  开始倒在 rails.js 上,想知道是否有任何 ajax: 回调被引发.

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

                  原来他们在 beforeSend 和错误...等一下...错误?这怎么可能?新待办事项的创建成功发生,响应是我期望的 JSON.但是在单步执行回调代码时,我注意到 Invalid label 错误.

                  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和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' }
                  

                  有点感谢 Trevor Burnham(就像这本书 BTW)的帮助和来自 sequence media 的 Amy,他的博文最终给了我解决办法.

                  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:成功处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)

                  • <legend id='Vzoij'><style id='Vzoij'><dir id='Vzoij'><q id='Vzoij'></q></dir></style></legend>

                    <small id='Vzoij'></small><noframes id='Vzoij'>

                      <bdo id='Vzoij'></bdo><ul id='Vzoij'></ul>
                      <i id='Vzoij'><tr id='Vzoij'><dt id='Vzoij'><q id='Vzoij'><span id='Vzoij'><b id='Vzoij'><form id='Vzoij'><ins id='Vzoij'></ins><ul id='Vzoij'></ul><sub id='Vzoij'></sub></form><legend id='Vzoij'></legend><bdo id='Vzoij'><pre id='Vzoij'><center id='Vzoij'></center></pre></bdo></b><th id='Vzoij'></th></span></q></dt></tr></i><div id='Vzoij'><tfoot id='Vzoij'></tfoot><dl id='Vzoij'><fieldset id='Vzoij'></fieldset></dl></div>
                        <tfoot id='Vzoij'></tfoot>

                            <tbody id='Vzoij'></tbody>