如何在轨道上的红宝石中使用带有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 联系我们: