views:

82

answers:

1

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.

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

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].

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.

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?

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

2.how can i debug this problem?

3.how can i get it to work?

+1  A: 

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

Here is the one I use: http://www.json.org/json2.js

So upon success you'll do something like:

var stuff = json.parse(returnedJSON)
Zachary
cant get it to work, can you give me an example?thank you
fenec
what's happening when you try to parse it?
Zachary
nothing shows up, it seem like its a nil object
fenec
try update_multiple(request.body)
Zachary
if i do alert(json.form_message); i get undefined in my dialog box
fenec
update_multiple(request.body) didnt change anything
fenec
i have fixed it with update_multiple(request.responseJSON)
fenec