如何在軌道上的紅寶石中使用帶有json的ajax

[英]how to use ajax with json in ruby on rails


I am implemeting a facebook application in rails using facebooker plugin, therefore it is very important to use this architecture if i want to update multiple DOM in my page. if my code works in a regular rails application it would work in my facebook application.

我正在使用facebooker插件在rails中實現facebook應用程序,因此如果我想在我的頁面中更新多個DOM,使用這種架構非常重要。如果我的代碼在常規rails應用程序中工作,它將在我的Facebook應用程序中工作。

i am trying to use ajax to let the user know that the comment was sent, and update the comments bloc.

我正在嘗試使用ajax讓用戶知道評論已發送,並更新評論集團。

migration:

移民:

class CreateComments < ActiveRecord::Migration
 def self.up
    create_table :comments do |t|
      t.string :body

      t.timestamps
    end
  end

  def self.down
    drop_table :comments
  end
end

controller:

控制器:

class CommentsController < ApplicationController

  def index
    @comments=Comment.all
  end

 def create
@comment=Comment.create(params[:comment])

  if request.xhr?
      @comments=Comment.all
      render :json=>{:ids_to_update=>[:all_comments,:form_message],
               :all_comments=>render_to_string(:partial=>"comments" ),
               :form_message=>"Your comment has been added." }
  else
    redirect_to comments_url
  end
 end

end

結束

view:

視圖:

<script>
function update_count(str,message_id) {
len=str.length;
if (len < 200) {
  $(message_id).innerHTML="<span style='color: green'>"+
    (200-len)+" remaining</span>";
} else {
  $(message_id).innerHTML="<span style='color: red'>"+
    "Comment too long. Only 200 characters allowed.</span>";
}
}


function update_multiple(json) {
for( var i=0; i<json["ids_to_update"].length; i++ ) {
    id=json["ids_to_update"][i];
    $(id).innerHTML=json[id];
}
}

</script>

<div id="all_comments" >
 <%= render :partial=>"comments/comments" %>
</div>

Talk some trash: <br />
<% remote_form_for Comment.new,
  :url=>comments_url,
  :success=>"update_multiple(request)" do |f|%>
<%= f.text_area :body,
 :onchange=>"update_count(this.getValue(),'remaining');" ,
 :onkeyup=>"update_count(this.getValue(),'remaining');"
%> <br />
<%= f.submit 'Post'%>
<% end %>

<p id="remaining" >&nbsp;</p>
<p id="form_message" >&nbsp;</p>
<br><br>
<br>

if i try to do alert(json) in the first line of the update_multiple function , i got an [object Object].

如果我嘗試在update_multiple函數的第一行做警報(json),我得到一個[object Object]。

if i try to do alert(json["ids_to_update"][0]) in the first line of the update_multiple function , there is no dialog box displayed.

如果我嘗試在update_multiple函數的第一行中執行警報(json [“ids_to_update”] [0]),則不會顯示任何對話框。

however the comment got saved but nothing is updated.

但是評論得到了保存但沒有更新。

questions:

問題:

1.how can javascript and rails know that i am dealing with json objects?deos ROR sent it a object format or a text format?

1.如何javascript和rails知道我正在處理json對象嗎?deos ROR發送了一個對象格式還是文本格式?

2.how can i see what is the returned json?do i have to parse it?how?

我怎么能看到返回的json是什么?我必須解析它嗎?怎么樣?

2.how can i debug this problem?

2.我可以調試這個問題嗎?

3.how can i get it to work?

我怎么能讓它上班?

1 个解决方案

#1


1  

You have to parse the returned JSON in your javascript using a JSON parser.

您必須使用JSON解析器在javascript中解析返回的JSON。

Here is the one I use: https://github.com/douglascrockford/JSON-js/blob/master/json2.js

這是我使用的那個:https://github.com/douglascrockford/JSON-js/blob/master/json2.js

So upon success you'll do something like:

所以一旦成功,你會做一些事情:

var stuff = json.parse(returnedJSON)

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/04/15/afb74925ccf5a1853593bc5fdd2a9804.html



 
  © 2014-2022 ITdaan.com 联系我们: