Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在RubyonRails中使用ajax和json_Javascript_Ruby On Rails_Ajax_Json - Fatal编程技术网

Javascript 如何在RubyonRails中使用ajax和json

Javascript 如何在RubyonRails中使用ajax和json,javascript,ruby-on-rails,ajax,json,Javascript,Ruby On Rails,Ajax,Json,我正在使用facebooker插件在rails中实现一个facebook应用程序,因此,如果我想更新页面中的多个DOM,那么使用这种架构是非常重要的。 如果我的代码在一个常规的rails应用程序中工作,它将在我的facebook应用程序中工作 我试图使用ajax让用户知道已发送评论,并更新评论组 迁移: class CreateComments < ActiveRecord::Migration def self.up create_table :comments do |t|

我正在使用facebooker插件在rails中实现一个facebook应用程序,因此,如果我想更新页面中的多个DOM,那么使用这种架构是非常重要的。 如果我的代码在一个常规的rails应用程序中工作,它将在我的facebook应用程序中工作

我试图使用ajax让用户知道已发送评论,并更新评论组

迁移:

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
class CreateComments
控制器:

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
class CommentsController{:ids\u to\u update=>[:all\u comments,:form\u message],
:all_comments=>render_to_string(:partial=>“comments”),
:form_message=>“您的评论已添加。”}
其他的
将\u重定向到注释\u url
结束
结束
结束

视图:


函数更新计数(str、消息id){
len=str.长度;
如果(透镜<200){
$(message_id).innerHTML=“”+
(200 len)+“剩余”;
}否则{
$(message_id).innerHTML=“”+
“注释太长。只允许200个字符。”;
}
}
函数更新(json){
对于(var i=0;i
说些废话:
评论(网址), :success=>“更新多个(请求)”do | f |%> “更新_计数(this.getValue(),'remaining');”, :onkeyup=>“更新计数(this.getValue(),'remaining');” %>




如果我尝试在update_multiple函数的第一行执行alert(json),我会得到一个[object]

如果我尝试在update_multiple函数的第一行执行警报(json[“ids_to_update”][0]),则不会显示任何对话框

但是,已保存注释,但未更新任何内容

问题:

1.javascript和rails如何知道我在处理json对象?deos ROR向其发送了对象格式或文本格式

2.如何查看返回的json?是否必须解析它?如何解析

2.如何调试此问题


3.如何使其工作?

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

下面是我使用的一个:

因此,一旦成功,您将采取以下措施:

var stuff = json.parse(returnedJSON)

当您试图解析它时发生了什么?如果我发出警报(json.form_message),我将在对话框中未定义
var stuff = json.parse(returnedJSON)