title: how to use ajax with json in ruby on rails
body: <p>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.</p>
  
  <p>i am trying to use ajax to let the user know that the comment was sent, and update the comments bloc.</p>
  
  <p>migration:</p>
  
  <pre><code>class CreateComments &lt; 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
  </code></pre>
  
  <p>controller:</p>
  
  <pre><code>class CommentsController &lt; ApplicationController
  
    def index
      @comments=Comment.all
    end
  
   def create
  @comment=Comment.create(params[:comment])
  
    if request.xhr?
        @comments=Comment.all
        render :json=&gt;{:ids_to_update=&gt;[:all_comments,:form_message],
                 :all_comments=&gt;render_to_string(:partial=&gt;"comments" ),
                 :form_message=&gt;"Your comment has been added." }
    else
      redirect_to comments_url
    end
   end
  </code></pre>
  
  <p>end</p>
  
  <p>view:</p>
  
  <pre><code>&lt;script&gt;
  function update_count(str,message_id) {
  len=str.length;
  if (len &lt; 200) {
    $(message_id).innerHTML="&lt;span style='color: green'&gt;"+
      (200-len)+" remaining&lt;/span&gt;";
  } else {
    $(message_id).innerHTML="&lt;span style='color: red'&gt;"+
      "Comment too long. Only 200 characters allowed.&lt;/span&gt;";
  }
  }
  
  
  function update_multiple(json) {
  for( var i=0; i&lt;json["ids_to_update"].length; i++ ) {
      id=json["ids_to_update"][i];
      $(id).innerHTML=json[id];
  }
  }
  
  &lt;/script&gt;
  
  &lt;div id="all_comments" &gt;
   &lt;%= render :partial=&gt;"comments/comments" %&gt;
  &lt;/div&gt;
  
  Talk some trash: &lt;br /&gt;
  &lt;% remote_form_for Comment.new,
    :url=&gt;comments_url,
    :success=&gt;"update_multiple(request)" do |f|%&gt;
  &lt;%= f.text_area :body,
   :onchange=&gt;"update_count(this.getValue(),'remaining');" ,
   :onkeyup=&gt;"update_count(this.getValue(),'remaining');"
  %&gt; &lt;br /&gt;
  &lt;%= f.submit 'Post'%&gt;
  &lt;% end %&gt;
  
  &lt;p id="remaining" &gt;&amp;nbsp;&lt;/p&gt;
  &lt;p id="form_message" &gt;&amp;nbsp;&lt;/p&gt;
  &lt;br&gt;&lt;br&gt;
  &lt;br&gt;
  </code></pre>
  
  <p>if i try to do  alert(json) in the first line of the update_multiple function , i got an [object Object].</p>
  
  <p>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.</p>
  
  <p>however the comment got saved but nothing is updated.</p>
  
  <p>it seems like the object sent by rails is nil or cant be parsed by JSON.parse(json).</p>
  
  <p>questions:</p>
  
  <p>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?how can it check that the json object has been sent</p>
  
  <p>2.how can i see what is the returned json?do i have to parse it?how?</p>
  
  <p>2.how can i debug this problem?</p>
  
  <p>3.how can i get it to work?</p>